Java小练习

1、输出乘法表

 1 public class Test {
 2     public static void main(String[] args) {
 3 
 4         int a, b, c;
 5         for (a = 1; a < 10; a++) {
 6             for (b = 1; b <= a; b++) {
 7                 c = a * b;
 8                 System.out.print(a + "*" + b + "=" + c);
 9                 System.out.print(" ");
10             }
11             System.out.println();
12         }
13 }

2、打印水仙花数(水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身)

 1 public class Test {
 2     public static void main(String[] args) {
 3 
 4         int e, d, f, g;
 5         for (e = 100; e < 1000; e++) {
 6             d = e / 100;
 7             f = e / 10 % 10;
 8             g = e % 10;
 9             if (e == d * d * d + f * f * f + g * g * g)
10                 System.out.println(e);
11         }
12 }

3、找出所有4位数的9倍是该数的反序数,例如1089的4倍是9801

 1 public class Test {
 2     public static void main(String[] args) {
 3         int h, j, k, l, m;
 4         for (m = 1000; m < 10000; m++) {
 5             h = m / 1000;
 6             j = m / 100 % 10;
 7             k = m / 10 % 100;
 8             l = m % 10;
 9             if (m * 9 == l * 1000 + k * 100 + j * 10 + h)
10                 System.out.println(m);
11         }
12 }

4、/**

 * 输出100-100000之间的所有的回文数。例如:121 131 141 1221 2552 12321 23432都是回文数。
问题分析:所谓的回文数,即左右对对称的数字。本体中可以分为3位数字,4位数字,5位数字。
(1)3位数,只要个位和百位数字相同即为回文数。
(2)4位数,要个位和千位数字相同,并且十位数字和百位数字相同。
(3)5位数,以百位数字位界限,左右两边的数字相同,其他高位数字以此类推。
*/
 1 package com.hsjry.I;
 2 
 3 import java.util.Scanner;
 4 
 5 /**
 6  * 输出100-100000之间的所有的回文数。例如:121 131 141 1221 2552 12321 23432都是回文数。
 7   问题分析:所谓的回文数,即左右对对称的数字。本体中可以分为3位数字,4位数字,5位数字。
 8  (1)3位数,只要个位和百位数字相同即为回文数。
 9  (2)4位数,要个位和千位数字相同,并且十位数字和百位数字相同。
10  (3)5位数,以百位数字位界限,左右两边的数字相同,其他高位数字以此类推。
11  */
12 public class TestCase1 {
13     public static void main(String[]args){
14         int a=0;
15         //判断输入的数字是否符合回文数
16         Scanner input =new Scanner(System.in);
17         a=input.nextInt();
18         if(a>100 && a<1000 && a/100==a%10){
19             System.out.println(a);
20         }
21         else if(a>=1000 && a<10000 && a/1000==a%10 && (a/100)%10==(a%100)/10){
22             System.out.println(a);
23         }
24         else if(a>=10000 && a<=100000 && a/10000==a%10 && (a/1000)%10==
25                 (a%100)/10%10){
26             System.out.println(a);
27         }
28         else {
29             System.out.println("无效");
30         }
31         /**
32         //输出符合这样的回文数
33         for (a=100;a<=100000;a++){
34             if(a>100 && a<1000 && a/100==a%10){
35                 System.out.println(a);
36             }
37             else if(a>=1000 && a<10000 && a/1000==a%10 && (a/100)%10==(a%100)/10){
38                 System.out.println(a);
39             }
40             else if(a>=10000 && a<=100000 && a/10000==a%10 && (a/1000)%10==
41                     (a%100)/10%10){
42                 System.out.println(a);
43             }
44         }*/
45 
46     }
47 }

5、查询水果价格

给定四种水果,分别是苹果(apple)、梨(pear)、桔子(orange)、葡萄(grape),单价分别对应为3.00元/公斤、2.50元/公斤、4.10元/公斤、10.20元/公斤。
首先在屏幕上显示以下菜单:
[1] apple
[2] pear
[3] orange
[4] grape
[0] exit
用户可以输入编号1~4查询对应水果的单价,用户输入0即退出;输入其他编号,显示此水果没有出售。

 1 package com.hsjry.I;
 2 
 3 import java.util.Scanner;
 4 
 5 /**
 6  *  查询水果价格
 7  给定四种水果,分别是苹果(apple)、梨(pear)、桔子(orange)、葡萄(grape)
 8  单价分别对应为3.00元/公斤、2.50元/公斤、4.10元/公斤、10.20元/公斤。
 9  首先在屏幕上显示以下菜单:
10  [1] apple
11  [2] pear
12  [3] orange
13  [4] grape
14  [0] exit
15  用户可以输入编号1~4查询对应水果的单价,用户输入0即退出;输入其他编号,显示此水果没有出售。
16  */
17 public class TestCase {
18     Scanner input=new Scanner(System.in);
19     public static void main(String[]args){
20         System.out.println("[1] apple");
21         System.out.println("[2] pear");
22         System.out.println("[3] orange");
23         System.out.println("[4] grape");
24         System.out.println("[0] exit");
25 
26         Scanner input=new Scanner(System.in);
27         boolean isLoop=true;
28         while (true){
29             int n=input.nextInt();
30             switch (n){
31                 case 0:
32                     isLoop=false;
33                     break;
34                 case 1:{
35                     System.out.println("3.00元/公斤");
36                     break;
37                 }
38                 case 2: {
39                     System.out.println("2.50元/公斤");
40                     break;
41                 }
42                 case 3: {
43                     System.out.println("4.10元/公斤");
44                     break;
45                 }
46                 case 4: {
47                     System.out.println("10.20元/公斤");
48                     break;
49                 }
50                 default:{
51                     System.out.println("水果没有出售");
52                     break;
53                 }
54             }
55         }
56     }
57 }

6、从键盘输入一行字符串(以换行符结束),要求分别统计里面英文字符的总个数和数字的总个数,并分别输出.

 1 package com.hsjry.I;
 2 
 3 import java.util.Scanner;
 4 
 5 public class TestCase2 {
 6     public static void main(String[] args) {
 7         Scanner input = new Scanner(System.in);
 8 
 9         char[] arr = input.nextLine().toCharArray();
10         int str = arr.length;
11         int word = 0, num = 0;
12 
13         while (str-->0) {
14             char c=arr[arr.length-str-1];
15             if (c>='a'&c<='z'|c>='A'&c<='Z') {
16                 word++;
17             } else if (c>='0'&c<='9') {
18                 num++;
19             }
20         }
21         System.out.println("英文字符的个数:"+word);
22         System.out.println("数字字符的个数:"+num);
23         input.close();
24     }
25 }

7、打印倒三角图案,要求从建盘输入整数n作为行数

 1 package com.hsjry.I;
 2 
 3 import java.util.Scanner;
 4 
 5 /**
 6  * 打印倒三角图案,要求从建盘输入整数n作为行数
 7  */
 8 public class TestCase3 {
 9     public static void main(String[]args){
10         Scanner input =new Scanner(System.in);
11         int a=input.nextInt();
12         int b=0;
13         int c=0;
14         //打印第一行
15         for(c=a;c>0;c--){
16             for (b=1;b<=c*2-1;b++){
17                 System.out.print("*");
18             }
19             //换行
20             System.out.println();
21             //打印空格
22             for (int i=0;i<=a-c;i++){
23                 System.out.print(" ");
24             }
25         }
26     }
27 }

8、36人搬72块砖。男一人搬4块,女一人搬3块,小孩两人搬一块,一次性搬完,问有多少种搬法?分别输出男,女,小孩的人数。
(递归算法)

 1 package com.hsjry.I;
 2 
 3 /**
 4  * 36人搬72块砖。男一人搬4块,女一人搬3块,小孩两人搬一块,一次性搬完,问有多少种搬法?分别输出男,女,小孩的人数。
 5  (递归算法)
 6  */
 7 public class TestCase4 {
 8     public static void main(String[]args){
 9         int t=0;
10         for(int m=0;m<=36;m++){
11             for (int w=0;w<=36;w++){
12                 for (int c=0;c<=36;c++){
13                     if (m+w+c==36 && 4*m+3*w+c/2==72){
14                         t++;
15                         System.out.println(m+","+w+","+c);
16                     }
17                 }
18             }
19         }
20         System.out.println(t);
21     }
22 }

9、打印一个菱形

 1 public class ZHOU {
 2     public static void main(String[] args) {
 3         int ab,abc,f,g;
 4         for(ab=1;ab<5;ab++){
 5             for(f=4;f>ab;f--){
 6                 System.out.print(" ");
 7             }
 8             for (abc=1;abc<2*ab;abc++){
 9                 System.out.print("*");
10             }
11             for(g=4;g>ab;g--){
12                 System.out.print(" ");
13             }
14             System.out.println(" ");
15         }
16 }
原文地址:https://www.cnblogs.com/datacenter/p/11447396.html