第五周上机练习

1. 打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。(知识点:循环语句、条件语句)

 1 package pro1;
 2 import java.util.Scanner;
 3 public class test {
 4     public static void main(String[] args) {
 5         for (int i = 100; i < 1000; i++) {
 6             int ge = i % 10;
 7             int shi = i % 100 / 10;
 8             int bai = i / 100;
 9             if (ge * ge * ge + shi * shi * shi + bai * bai * bai == i) {
10                 System.out.println(i);
11             }
12         }
13     }
14 }

2.在控制台输出以下图形(知识点:循环语句、条件语句)

 1 package pro1;
 2 import java.util.Scanner;
 3 public class test {
 4     public static void main(String[] args) {
 5         for(int i=1;i<=6;i++){
 6             for(int j=1;j<=i;j++){
 7                 System.out.print(j);
 8             }
 9             System.out.println(); 
10         }
11     }
12 }
 1 package pro1;
 2 import java.util.Scanner;
 3 public class test {
 4     public static void main(String[] args) {
 5         for(int i=6;i>=0;i--){
 6             for(int j=1;j<=i;j++){
 7                 System.out.print(j);
 8             }
 9             System.out.println(); 
10         }
11     }
12 }
 1 package pro1;
 2 import java.util.Scanner;
 3 public class test {
 4     public static void main(String[] args) {
 5         for(int i=1;i<7;i++) {
 6             for (int k = 1; k <=6-i ; k++) {
 7                 System.out.print(" ");
 8             }
 9             for(int j=i;j>0;j--) {
10                 System.out.print(j);
11             }
12             System.out.println();
13         }
14     }
15 }
 1 package pro1;
 2 import java.util.Scanner;
 3 public class test {
 4     public static void main(String[] args) {
 5         for(int i = 6;i >= 1;i--) {
 6             for(int k = 0;k < 6-i;k++) {
 7                 System.out.print(" ");
 8             }
 9             for(int j = 1;j <= i;j++) {
10                 System.out.print(j);
11             }
12             System.out.println("");
13 
14         }
15     }
16 }

3. 输入年月日,判断这是这一年中的第几天(知识点:循环语句、条件语句)

 1 package pro1;
 2 import java.util.Scanner;
 3 public class test {
 4     public static void main(String[] args) {
 5         int year,month,day,sum=0,eryue=28;
 6         Scanner in = new Scanner(System.in);
 7         System.out.print("请输入年份:");
 8         year = in.nextInt();
 9         System.out.print("请输入月份:");
10         month = in.nextInt();
11         System.out.print("请输入几号:");
12         day = in.nextInt();
13         if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
14         {
15             eryue = 29;
16         }        
17         switch(month)
18             {
19                     case 1:
20                         sum = day;
21                         break;
22                     case 2:
23                         sum = 31 + day;
24                         break;
25                     case 3:
26                         sum = 31+eryue+day;
27                         break;
28                     case 4:
29                         sum = 31+eryue+31+day;
30                         break;
31                     case 5:
32                         sum = 31+eryue+31+30+day;
33                         break;
34                     case 6:
35                         sum = 31+eryue+31+30+31+day;
36                         break;
37                     case 7:
38                         sum = 31+eryue+31+30+31+30+day;
39                         break;
40                     case 8:
41                         sum = 31+eryue+31+30+31+30+31+day;
42                         break;
43                     case 9:
44                         sum = 31+eryue+31+30+31+30+31+31+day;
45                         break;
46                     case 10:
47                         sum = 31+eryue+31+30+31+30+31+31+30+day;
48                         break;
49                     case 11:
50                         sum = 31+eryue+31+30+31+30+31+31+30+31+day;
51                         break;
52                     case 12:
53                         sum = 31+eryue+31+30+31+30+31+31+30+31+30+day;
54         }
55         System.out.println(year+" 年 "+month+" 月 " + day + "号是当年的第"+sum+"天");
56            }
57 }

 

4.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321(知识点:循环语句、条件语句)

 1 package pro1;
 2 import java.util.Scanner;
 3 public class test {
 4     public static void main(String[] args) {
 5         Scanner aaa = new Scanner(System.in);
 6         System.out.println("请输入一个数:");
 7             int x = aaa.nextInt();
 8             if(x >= 1000 && x < 10000){
 9                 int ge = x % 10;
10                 int shi = x % 100 / 10;               
11                 int bai = x % 1000 / 100;
12                 int qian = x / 1000;
13                 int sum = qian + bai*10 +shi*100 +ge*1000;
14                 System.out.println(sum);
15             }
16      }
17 }

 

posted @ 2020-04-02 12:43  潘宇航  阅读(106)  评论(0编辑  收藏  举报