Fork me on GitHub

java 练习题

Posted on 2019-07-24 15:52  攀一座山  阅读(552)  评论(0)    收藏  举报

1.输入一个五位的正整数,输出其逆数。例如输入12345,输出应为54321

12345

12345/10000                1

12345/1000       12%10    2

12345/100        123%10   3

12345/10         1234%10  4

12345%10                   5

 1 public class 练习01 {
 2     public static void main(String[] args) {
 3 
 4         int h=12345;
 5         int j=10000;
 6         int sum=0;
 7         for ( int i = 1; i < 10001; i *= 10) {
 8             int a = h / i;
 9             int b = a % 10;
10             int c = b * j;
11             if (j > 1) {
12                 j /= 10;
13             }
14             sum = c + sum;
15         }System.out.println(sum);
16     }
17 
18 }

 

2.计算1+2+3…+n的值,n是从键盘输入的自然数。

public class 练习02 {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        int n=scanner.nextInt();
        int sum=0;
        for (int i=0;i<n+1;i++){
            sum=i+sum;
        }
        System.out.println(sum);
    }
}

 

3.从终端(键盘)读入20个数据到数组中,统计其中正数的个数,并计算这些正数之和。

 1 public class 练习03 {
 2 
 3     public static void main(String[] args) {
 4         Scanner scanner = new Scanner(System.in);
 5         int[] arr = new int[20];
 6         int sum = 0;
 7         int count=0;
 8         for (int i = 0; i < 20; i++) {
 9             arr[i] = scanner.nextInt();
10             if (arr[i] > 0) {
11                 sum += arr[i];
12                 count += 1;
13             }
14             }
15             System.out.println(sum);
16             System.out.println(count);
17         }
18     }

 

4.从终端(键盘)将5个整数输入到数组a中,然后将a逆序复制到数组b中,并输出b中各元素的值。

 1 public class 练习04 {
 2     public static void main(String[] args) {
 3         Scanner scanner = new Scanner(System.in);
 4         System.out.print("添加数组长度:");
 5         int h=scanner.nextInt();
 6         int[] a = new int[h];
 7         int[] b = new int[a.length];
 8         System.out.println("添加元素:");
 9         for (int i = 0; i < a.length; i++) {
10             a[i] = scanner.nextInt();
11         }
12         for (int j=0;j<a.length;j++){
13             b[j]=a[a.length-1-j];
14         }
15         for (int i=0;i<a.length;i++){
16             System.out.print(b[i]);
17         }
18     }
19 }

5.要将5100元的大钞票,换成等值的50元,20元,10元,5元一张的小钞票,每种面值至少1张,编程求需要多少张纸币。

 1 public class 练习5 {
 2     public static void main(String[] args) {
 3         int sum=0;
 4         int count=0;
 5         for (int a50=1;a50<10;a50++){
 6             for (int a20=1;a20<25;a20++){
 7                 for (int a10=1;a10<50;a10++){
 8                     for (int a5=1;a5<100;a5++){
 9                         if (a5*5+a10*10+a20*20+a50*50==500){
10 
11                             System.out.println("a5="+a5);
12                             System.out.println("a10="+a10);
13                             System.out.println("a20="+a20);
14                             System.out.println("a50="+a50);
15                             sum=a5+a10+a20+a50;
16                             count++;
17                             System.out.println("sum="+sum);
18                         }
19                     }
20                 }
21             }
22         } System.out.println("count"+count);
23     }
24 }

 

6.n以内(不包括n)同时能被37整除的所有自然数之和的平方根sn从键盘输入。例如若n1000时,函数值应为:s=153.909064

 1 public class 练习06 {
 2     public static void main(String[] args) {
 3         Scanner scanner=new Scanner(System.in);
 4         int n=scanner.nextInt();
 5         int sum=0;
 6         for (int i=0;i<=n;i++){
 7             if (i%3==0&i%7==0){
 8                 sum=sum+i;
 9             }
10         }
11         System.out.println("平方根为"+Math.sqrt(sum));
12     }
13 }

7.一辆卡车违反交通规则,撞人后逃跑。现场有三人目击事件,但都没有记住车号,只记下车号的一些特征。甲说:牌照的前两位数字是相同的;乙说:牌照的后两位数字是相同的,但与前两位不同;丙是数学家,他说:四位的车号刚好是一个整数的平方。请根据以上线索找出车号。

32*32  1024

33*33

34*34

99*99 

 1 public class 练习7 {
 2     public static void main(String[] args) {
 3         int a,a1,a2,a3,a4;
 4         for (int i=32;i<100;i++){
 5             a=i*i;
 6             a1=a/1000;
 7             a2=a/100%10;
 8             a3=a/10%10;
 9             a4=a%10;
10             if (a1==a2&a3==a4&a1!=a3){
11                 System.out.println(a);
12                 System.out.println(i);
13             }
14         }
15     }
16 }

8.个位数为6且能被3整除的三位自然数共有多少个,分别是哪些?

 1 public class 练习8 {
 2     public static void main(String[] args) {
 3         int count=0;
 4         for (int a=100;a<1000;a++){
 5             if (a%10==6&a%3==0){
 6                 System.out.println(a);
 7                 count+=1;
 8             }
 9         }
10         System.out.println(count);
11     }
12 }

 

9.输入某年某月某日,判断这一天是这一年的第几天?

 1 public class 练习9 {
 2     public static void main(String[] args) {
 3         Scanner scanner=new Scanner(System.in);
 4         System.out.println("请输入年份:");
 5         int a=scanner.nextInt();
 6         System.out.println("请输入月份:");
 7         int b=scanner.nextInt();
 8         System.out.println("请输入日期:");
 9         int c=scanner.nextInt();
10         result(a,b,c);
11     }
12 
13       public static void result(int year,int month,int day){
14         int data=30*(month-1)+day;
15         if (month>=11){
16             data=data+6-2;
17         }else if(month>=9){     //       9.15   前面13578    各加一天
18             data=data+5-2;
19         }else if(month>=8){
20             data=data+4-2;
21         }else if(month>=6){
22             data=data+3-2;
23         }else if(month>=4){
24             data=data+2-2;
25         }else if(month==3){       //三月减二月多算的2天。
26             data=data+1-2;
27         }else if(month==2){    //二月几号直接由day决定,不用减2天。
28             data=data+1;       //1月31天,加1天。
29         }
30           if(year%4==0&&year%100!=0){
31               //闰年二月加一天
32               data++;
33               System.out.print(year+"年"+month+"月"+day+"日是"+year+"年"+"第"+data+"天");
34           }else{
35               System.out.print(year+"年"+month+"月"+day+"日是"+year+"年"+"第"+data+"天");
36           }
37       };
38 }

 

10.两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。

 1 public class 练习10 {
 2     public static void main(String[] args) {
 3         for (int i=0;i<3;i++){
 4             for (int j=0;j<3;j++){
 5 
 6                if (i==0&j==0|i==2&j==0|i==2&j==2|i==0&j==1|i==1&j==1|i==1&j==2) {
 7 
 8                }else {
 9                 if (i==0){
10                     System.out.print("a");
11                 }else if (i==1){
12                     System.out.print("b");
13                 }else if (i==2){
14                     System.out.print("c");
15                 }
16                 if (j==0){
17                     System.out.println("x");
18                 }else if (j==1){
19                     System.out.println("y");
20                 }else if (j==2){
21                     System.out.println("z");
22                 }
23 
24           }}
25       }
26 
27     }
28 
29 }

 

11.有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后 问第一个人,他说是10岁。请问第五个人多大?

 1 public class 练习11 {
 2     public static void main(String[] args) {
 3         int a=10;
 4         int e= 0;
 5         for (int i=0;i<5;i++){
 6             e=a+2*i;
 7         }
 8         System.out.println(e);
 9     }
10 }

 

12.输入某三角形的三个边的长度,判断出这是个什么三角形(等腰、等边、任意,或不能构成)。

 1 public class 练习12 {
 2     public static void main(String[] args) {
 3         Scanner scanner=new Scanner(System.in);
 4         int[]arr=new int[3];
 5         for (int i=0;i<3;i++){
 6             arr[i]=scanner.nextInt();
 7         }if (arr[0]+arr[1]>arr[2]&arr[0]-arr[1]<arr[2]) {
 8             System.out.println("可以组成三角形");
 9             if(arr[0]==arr[1]&arr[0]==arr[2]){
10                 System.out.println("等边三角形");
11 
12             }else if (arr[0]==arr[1]|arr[0]==arr[2]|arr[1]==arr[2]){
13                 System.out.println("等腰三角形");
14             }else{
15                 System.out.println("任意三角形");
16             }
17         }else{
18             System.out.println("不能组成三角形");
19         }
20     }
21 
22 
23 }

13.输入10个数,分别统计其中正数、负数、零的个数。

 1 public class 练习13 {
 2     public static void main(String[] args) {
 3         Scanner scanner = new Scanner(System.in);
 4         int[]arr=new int[10];
 5         int zhengshu=0;
 6         int fushu=0;
 7         int ling=0;
 8         for (int i=0;i<arr.length;i++) {
 9             int a = scanner.nextInt();
10             arr[i]=a;
11             if (arr[i]>0){
12                 zhengshu+=1;
13             }else if (arr[i]==0){
14                 ling+=1;
15             }else if (arr[i]<0){
16                 fushu+=1;
17             }
18         }
19         System.out.println("正数个数="+zhengshu);
20         System.out.println("零的个数="+ling);
21         System.out.println("负数个数="+fushu);
22 
23     }
24 }

 

14.先随机产生N个三位自然数输出,然后再输出其中同时是357倍数的数。(设N100

 1 public class 练习14 {
 2     public static void main(String[] args) {
 3         Random random=new Random();
 4         for (int i=0;i<100;i++){
 5         int a=random.nextInt(1000-100)+100;
 6         if (a%3==0&&a%5==0&&a%7==0){
 7             System.out.println(a);
 8         }
 9     }
10     }
11 }

 

15.for编程找出100~200中的完全平方数。

 1 public class 练习15 {
 2     public static void main(String[] args) {
 3         for (int j=10;j<20;j++){
 4             for (int i=100;i<=200;i++){
 5                 if (Math.sqrt(i)==j){
 6                     System.out.println(i);
 7             }
 8         }
 9     }
10 }
11 }

 

16.从终端输入三个正数,判断这三个数能否构成直角三角形。

 1 public class 练习16 {
 2     public static void main(String[] args) {
 3         Scanner scanner=new Scanner(System.in);
 4         int[]arr=new int[3];
 5         for (int i=0;i<3;i++){
 6             int a=scanner.nextInt();
 7             arr[i]=a;
 8         }if (Math.pow(arr[0],2)+Math.pow(arr[1],2)==Math.pow(arr[2],2)||
 9         Math.pow(arr[0],2)+Math.pow(arr[2],2)==Math.pow(arr[1],2)||
10         Math.pow(arr[1],2)+Math.pow(arr[2],2)==Math.pow(arr[0],2)
11         ){
12             System.out.println("可以构成直角三角形");
13         }else {
14             System.out.println("不可以构成直角三角形");
15         }
16     }
17 }

 

17.随机产生N个大写字母输出,然后统计其中共有多少个元音字符。(设N50

 1 public class 练习17 {
 2     public static void main(String[] args) {
 3         Random random=new Random();
 4         int count=0;
 5         for (int i=0;i<50;i++) {
 6             int a = random.nextInt(91 - 65) + 65;
 7             char h=((char) a);
 8             if (h==((char)65)||h==((char)69)||h==((char)73)
 9             ||h==((char)79)||h==((char)85)) {
10                 System.out.println(h);
11                 count+=1;
12             }
13         }
14         System.out.println(count);
15     }
16 }

 

18.从键盘输入10个战士的身高,输出平均身高,并找出哪些身高高于平均身高。

 1 public class 练习18 {
 2     public static void main(String[] args) {
 3         Scanner scanner = new Scanner(System.in);
 4         int[] arr = new int[10];
 5         int sum = 0;
 6         for (int i = 0; i < arr.length; i++) {
 7             int a = scanner.nextInt();
 8             arr[i] = a;
 9             sum += arr[i];
10         }for (int i=0;i<arr.length;i++){
11         if (arr[i] > sum / 10) {
12             System.out.println("高于平均的有" + arr[i]);
13         }
14         if (arr[i] < sum / 10) {
15             System.out.println("低于平均的有" + arr[i]);
16         }
17     }
18     }
19 }

 

19.从键盘输入10个战士的身高,输出最高、最低的身高。

 1 public class 练习19 {
 2     public static void main(String[] args) {
 3         Scanner scanner = new Scanner(System.in);
 4         int[]arr=new int[10];
 5         for (int i = 0; i < arr.length; i++) {
 6             int a = scanner.nextInt();
 7             arr[i]=a;
 8         }
 9         for (int j=1;j<arr.length;j++){
10             if (arr[j]>arr[0]){
11                 int s=arr[j];
12                 arr[j]=arr[0];
13                 arr[0]=s;
14             }
15         }
16         for (int j=1;j<arr.length;j++){
17             if (arr[j]<arr[arr.length-1]){
18                 int s=arr[j];
19                 arr[j]=arr[arr.length-1];
20                 arr[arr.length-1]=s;
21             }
22         }
23         System.out.println("最高的是"+arr[0]);
24         System.out.println("最矮的是"+arr[arr.length-1]);
25     }
26 }

 

20.百钱百鸡问题。百钱买百鸡,鸡翁一值钱三,鸡母一值钱二,鸡雏三值钱一,问鸡翁、鸡母、鸡雏各几何?

 1 public class 练习20 {
 2     public static void main(String[] args) {
 3         int a=100;
 4         for (int i=0;i<35;i++){
 5             for (int j=0;j<=50;j++){
 6                 int c=100-i-j;
 7                 if (i+j+c==100&3*i+2*j+c/3==100){
 8                     System.out.println(" 公鸡 "+i+" 母鸡 "+j+" 小鸡 "+c);
 9                 }
10 
11             }
12         }
13     }
14 }

 

21.N是一个四位数,它的9倍恰好是其反序数,求N。反序数就是将整数的数字倒过来形成的整数。例如:1234的反序数是4321

 1 public class 练习21 {
 2     public static void main(String[] args) {
 3         for (int i=1;i<10;i++){
 4             for (int j=0;j<10;j++){
 5                 for (int k=0;k<10;k++){
 6                     for (int m=0;m<10;m++){
 7                         if ((i*1000+j*100+k*10+m)*9==m*1000+k*100+j*10+i){
 8                             System.out.println(i*1000+j*100+k*10+m);
 9                             System.out.println((i*1000+j*100+k*10+m)*9);
10                         }
11                     }
12                 }
13             }
14         }
15 
16     }
17 }

 

22.爱因斯坦出了一道这样的数学题:有一条长阶梯,若每步跨2阶,则最后剩一阶,若每步跨3 阶,则最后剩2阶,若每步跨5阶,则最后剩4阶,若每步跨6阶则最后剩5阶。只有每次跨7阶,最后才正好一阶不剩。请问这条阶梯至少有多少阶?

 1 public class 练习22 {
 2     public static void main(String[] args) {
 3         for (int i=7;;i+=7){
 4             if (i%2==1&&i%3==2&&i%5==4&&i%6==5&&i%7==0){
 5                 System.out.println(i);
 6                 break;
 7             }
 8         }
 9     }
10 }

 

23.随机生成10100以内的加减乘除数学题 回答正确的加10分错误不加分 然后显示成绩 

3+2=5    5  

Int a[10][5]

 1 public class 练习23 {
 2     public static void main(String[] args) {
 3         Scanner scanner=new Scanner(System.in);
 4         int f=0;
 5         for (int i=0;i<10;i++){
 6             Random random=new Random();
 7             int a=random.nextInt(100);
 8             int b=random.nextInt(4);
 9             int c=random.nextInt(100);
10             if (b==0){
11                 System.out.print(a+"+"+c+"=");
12                 int d=scanner.nextInt();
13                 if (a+c==d){
14                     f+=10;
15                 }
16             } else if (b==1){
17                 System.out.print(a+"-"+c+"=");
18                 int d=scanner.nextInt();
19                 if (a-c==d){
20                     f+=10;
21                 }
22             } else if (b==2){
23                 System.out.print(a+"*"+c+"=");
24                 int d=scanner.nextInt();
25                 if (a*c==d){
26                     f+=10;
27                 }
28             } else if (b==3) {
29                 System.out.print(a + "/" + c + "=");
30                 int d = scanner.nextInt();
31                 if (a / c == d) {
32                     f += 10;
33                 }
34             }
35 
36 
37         }System.out.println(f);
38 
39 
40     }
41 }

24.1000发子弹 要提前装道10箱子里面,接收键盘输入,要取多少颗子弹数,只能显示整箱的个数,问这10个箱子怎么装(定义一个数组10个元素,分别装子弹的个数,比如取走100发子弹 程序运行结果,比如2箱)

 

1       2       4     8       16      

2^0   2^1

32     64

 1 public class 练习24 {
 2     public static void main(String[] args) {
 3         int[]arr=new int[10];
 4         int sum=0;
 5         for (int i=0;i<arr.length-1;i++){
 6             arr[i]=(int) (Math.pow(2,i));
 7             sum+=arr[i];
 8             System.out.println(arr[i]);
 9         }
10         arr[arr.length-1]=1000-sum;
11         System.out.println(arr[arr.length-1]);
12     }
13 }

题目: 12344 个数字,能组成多少个互不相同且无重复数字的三位数? 都是多少?

 1 public class 无重复数字 {
 2     public static void main(String[] args) {
 3         int[]arr={1,2,3,4};
 4         for (int i=0;i<4;i++){
 5             for (int j=0;j<4;j++){
 6                 for (int k=0;k<4;k++){
 7                     if (arr[i]!=arr[j]&&arr[i]!=arr[k]&&arr[j]!=arr[k]){
 8 
 9                      System.out.println(arr[i]*100+arr[j]*10+arr[k]);
10                     }
11                 }
12             }
13         }
14     }
15 }

题目:一个整数,它加上 100 后是一个完全平方数,再加上 168 又是一个完全平 方数,请问该数是多少

 1 public class 完全平方数 {
 2     public static void main(String[] args) {
 3         for (int j=0;j<300;j++)
 4             for (int k=0;k<300;k++)
 5                 for (int i=0;i<300;i++) {
 6                     if (Math.sqrt(j + 100)==k&Math.sqrt(j + 100 + 168) ==i) {
 7                     System.out.println(j);
 8                     break;
 9                 }
10             }
11         }
12     }

题目:输出 9*9 口诀。

 1 public class 乘法表 {
 2     public static void main(String[] args) {
 3     for(int i=1;i<=9;i++) {
 4         for (int j=1;j<=i;j++){
 5             System.out.print(i+"*"+j+"="+i*j);
 6 //            System.out.print("*");
 7         }
 8         System.out.println();
 9     }
10     }
11 }

题目:古典问题:有一对兔子,从出生后第 3 个月起每个月都生一对兔子,小兔 子长到第三个月

后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?

 1 public class 兔子生兔问题 {
 2     public static void main(String[] args) {
 3         int yue=0;
 4         int shangyue=2;
 5         int shangshangyue=2;
 6         int dangqianyue=0;
 7         Scanner scanner=new Scanner(System.in);
 8         System.out.println("请输入月份:");
 9         yue=scanner.nextInt();
10         if (yue==1 || yue==2)
11         {
12             System.out.println("2");
13             return;
14         }
15         for (int i=3;i<=yue;i++)
16         {
17             dangqianyue=shangshangyue+shangyue;
18             shangshangyue=shangyue;
19             shangyue=dangqianyue;
20         }
21         System.out.println(dangqianyue);
22     };
23 }

题目:打印出所有的水仙花数,所谓水仙花数是指一个三位数,其各位数字 立方和等于该数

 1 public class 水仙花数 {
 2     public static void main(String[] args) {
 3         int a,b,c,d,e,f;
 4         for (int i=100;i<1000;i++)
 5         {
 6             a = i/100;
 7             b = i / 10 % 10;
 8             c = i %10;
 9             if (a*a*a+b*b*b+c*c*c==i){
10                 System.out.println(i);
11             }
12         }
13     }
14 }

题目:一球从 100 米高度自由落下,每次落地后反跳回原高度的一半;再落下, 求它在

10 次落地时,共经过多少米? 10 次反弹多高

 1 public class 球落地 {
 2     public static void main(String[] args) {
 3         int a=100;
 4         int sum=100;
 5         for (int i=2;i<=10;i++){
 6             sum=sum+a;
 7             a=a/2;
 8         }
 9         System.out.println(sum);
10     }
11 }

题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾, 多吃了一个

第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃

了前一天剩下的一半零一个。到第 10 天早上想再吃时,见只剩下一个桃子了。求第一

天共摘了多少。

1 public class 猴子吃桃问题 {
2     public static void main(String[] args) {
3         int sum=1;
4         for (int j = 0; j < 9; j++) {
5             sum= (sum + 1)*2;
6                 }
7         System.out.println(sum);
8         };
9     }

题目:输入某年某月某日,判断这一天是这一年的第几天?

1.程序分析: 3 5 日为例,应该先把前两个月的加起来,然后再加上 5 天即 本年的第几天,特殊

情况,闰年且输入月份大于 3 时需考虑多加一天。

 1 public class 练习10 {
 2     public static void main(String[] args) {
 3         Scanner scanner=new Scanner(System.in);
 4         System.out.println("请输入年份:");
 5         int a=scanner.nextInt();
 6         System.out.println("请输入月份:");
 7         int b=scanner.nextInt();
 8         System.out.println("请输入日期:");
 9         int c=scanner.nextInt();
10         result(a,b,c);
11     }
12 
13       public static void result(int year,int month,int day){
14         int data=30*(month-1)+day;
15         if (month>=11){
16             data=data+6-2;
17         }else if(month>=9){     //       9.15   前面13578    各加一天
18             data=data+5-2;
19         }else if(month>=8){
20             data=data+4-2;
21         }else if(month>=6){
22             data=data+3-2;
23         }else if(month>=4){
24             data=data+2-2;
25         }else if(month==3){       //三月减二月多算的2天。
26             data=data+1-2;
27         }else if(month==2){    //二月几号直接由day决定,不用减2天。
28             data=data+1;       //1月31天,加1天。
29         }
30           if(year%4==0&&year%100!=0){
31               //闰年二月加一天
32               data++;
33               System.out.print(year+"年"+month+"月"+day+"日是"+year+"年"+"第"+data+"天");
34           }else{
35               System.out.print(year+"年"+month+"月"+day+"日是"+year+"年"+"第"+data+"天");
36           }
37       };
38 }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3