第五次上机作业

1.打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。

 1 package test1;
 2 
 3 public class test2 {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         for(int i=100;i<=999;i++){
 8             int a=i/100;
 9             int b=i%100/10;
10             int c=i%10; 
11             if(a*a*a+b*b*b+c*c*c==i){
12             System.out.println("水仙花数为:"+i);
13             }
14             
15         }
16     }
17 
18 }

2.在控制台输出以下图形

 1 package test1;
 2 
 3 public class test2 {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         for(int i = 1;i <= 6;i++) {
 8             for(int j = 0;j < i;j++) {
 9                 System.out.print("*");
10             }
11             System.out.println();
12         }
13     }
14 
15 }
 1 package test1;
 2 
 3 public class test2 {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         for(int i = 0;i < 6;i++) {
 8             for(int j = i;j < 6;j++) {
 9                 System.out.print("*");
10             }
11             System.out.println();
12         }
13     }
14 
15 }
package test1;

public class test2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        for(int i = 1;i <= 6;i++) {
            for(int j = 1;j <= 6-i;j++) {
                System.out.print(" ");
            }
            for(int j = 1;j <= i;j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }

}
 1 package test1;
 2 
 3 public class test2 {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         for(int i = 1;i <= 6;i++) {
 8             for(int j = 1;j <= i-1;j++) {
 9                 System.out.print(" ");
10             }
11             for(int j = 1;j <= 7-i;j++) {
12                 System.out.print("*");
13             }
14             System.out.println();
15         }
16     }
17 
18 }

3.输入年月日,判断这是这一年中的第几天

 1 package test1;
 2 import java.util.Scanner;
 3 public class test2 {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         System.out.println("请分别输入一个四位数的值:");
 8         Scanner input=new Scanner(System.in);
 9         System.out.println("year:");
10         System.out.println("month:");
11         System.out.println("days:");
12         
13         int year=input.nextInt();
14         int month=input.nextInt();
15         int days=input.nextInt();
16         
17         int X = 0;
18         for(int i = 1;i < month;i++) {
19             switch(i) {
20             case 4:
21             case 6:
22             case 9:
23             case 11:
24                 X += 30;
25                 break;
26             case 2:
27                 if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
28                     X += 29;
29                 }else
30                     X += 28;
31                 break;
32             default: X += 31;
33             break;
34                 }
35             }
36         X += days;
37         System.out.println("这天是今年的第"+ X +"天");
38         }
39     }

4.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321

 1 package test1;
 2 import java.util.Scanner;
 3 public class test2 {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         System.out.println("请分别输入一个四位数的值:");
 8         Scanner input=new Scanner(System.in);
 9         int N=input.nextInt();
10         int a,b,c,d,e;
11         a=N/1000;
12         b=N/100%10;
13         c=N/10%10;
14         d=N%10;
15         e=d*1000+c*100+b*10+a;
16         System.out.println("反转后为"+e);
17     }
18 
19 }

 

posted @ 2020-04-02 14:49  就这水平?  阅读(118)  评论(0)    收藏  举报