分享几个Java编程运算具体实例
2021/5/21 12:29:49
本文主要是介绍分享几个Java编程运算具体实例,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
分享几个Java编程运算具体实例
1、编写一个方法func2(),判断一个整数是否包含数字7.
import java.util.Scanner;
public class Test3 {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.println("请输入数值: ");
int num = sc.nextInt();
System.out.println(func2( num));
}
public static boolean func2(int num){
int n;
boolean flag=false;
while(num!=0){
n=num%10;
if(n==7){
flag=true;
break;
}
num /= 10;
}
return flag;
}
}
2.编写一个类Demo,类里有一个方法metod2(),对输入的任意字符串“1,4,7,13,5,17,9”转换为数组元素是整数元素,需要保存在整形数组中且实现排序输出“1 4 5 7 9 13 17”
package oracle.zibo.exam;
import java.util.Arrays;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
// String str =("1,4,7,13,5,17,9");
Scanner sc = new Scanner(System.in);
System.out.println("请输入数字字符串,并用逗号隔开");
String str = sc.next();
metod2(str);
}
public static void metod2(String str){
String array[] = str.split(",");
int p[]=new int[array.length];
for(int i=0;i<array.length;i++){
p[i]=Integer.parseInt(array[i]);
}
//Arrays.sort(p);
for(int i=0;i<p.length-1;i++){
for(int j=0;j<p.length-1-i;j++){
if(p[j]>p[j+1]){
int temp =p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
}
}
for(int i=0;i<p.length;i++){
System.out.print(p[i]+" ");
}
}
}
3.假设有字符串“uyde87344USijnmWO987”,统计其中数字的个数、大写字母的个数、小写字母的个数.
package oracle.zibo.exam;
import java.util.Scanner;
public class Test7 {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.println("请输入字符串 ");
String str=sc.next();
getnum(str);
}
public static void getnum(String str){
int cU = 0 ;
int cL = 0 ;
int cN = 0 ;
char c ;
for(int i=0 ;i<str.length() ;i++) {
c = str.charAt(i) ;
if(c >='a' && c <= 'z') {
cL ++ ;
}
else if(c >= 'A' && c <= 'Z') {
cU ++ ;
}
else if(c>='0'&&c<='9'){
cN ++ ;
}
}
System.out.println("小写字母个数为:" + cL) ;
System.out.println("大写字母个数为:" + cU) ;
System.out.println("数字个数为:" + cN) ;
}
}
4,编写一个方法,传入数值,打印出金字塔
public static void print(int num){
for(int i=0;i<num;i++){
for(int k=0;k<num-i-1;k++){
System.out.print(" ");
}
for(int j=0;j<2*i+1;j++){
System.out.print("*");
}
System.out.println();
}
}
这篇关于分享几个Java编程运算具体实例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-07-04TiDB 资源管控的对撞测试以及最佳实践架构
- 2024-07-03万字长文聊聊Web3的组成架构
- 2024-07-02springboot项目无法注册到nacos-icode9专业技术文章分享
- 2024-06-26结对编程到底难不难?答案在这里
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现