第五次Java作业

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

 1 package test;
 2 public class one {
 3 
 4     public static void main(String[] args) {
 5         // TODO Auto-generated method stub
 6         int i=100,g,s,b;
 7         while(i<=999){
 8             g=i%10;
 9             s=i%100/10;
10             b=i/100;
11             if(g*g*g+s*s*s+b*b*b==i){
12                 System.out.println(i);
13                 }
14                 i++;
15         }
16         }
17 }

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

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

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

 1 package test;
 2 import java.util.*;
 3 public class one {
 4     public static void main(String[] args) {
 5         // TODO Auto-generated method stub
 6          Scanner input=new Scanner(System.in);
 7          System.out.println("请输入年:");
 8          int year=input.nextInt();
 9          System.out.println("请输入月:");
10          int month=input.nextInt();
11          System.out.println("请输入日:");
12          int day=input.nextInt();
13          int total=0;//统计输入月份之前一共多少天
14          for(int i=1;i) {
15              switch(i){
16              case 4:
17              case 6:
18              case 9:
19              case 11:
20                  total+=day;
21                     break;
22                    case 2:
23                     if(year%4==0&&year%100!=0||year%400==0)
24                      total+=29;
25                     else
26                      total+=28;
27                     break;
28                     default:
29                     total+=31;
30                     break;
31                    }                                        
32                    }
33                   total+=31;
34                   System.out.println("该天是第"+total+"天");
35                  }

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

 1 package test;
 2 import java.util.Scanner;
 3 public class one {
 4     public static void main(String[] args) {
 5         // TODO Auto-generated method stub
 6           Scanner sc=new Scanner(System.in);
 7             System.out.println("请输入一个四位数");
 8             int i=sc.nextInt();
 9             int fz;
10             if(i>999&&i<=9999) {
11              fz=i/1000+i/100%10*10+i/10%10*100+i%10*1000;
12              System.out.print(fz);
13             }else {
14              System.out.print("错误!");
15            
16             }
17             
18         }
19     }

 

你可能感兴趣的:(第五次Java作业)