1. 打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。(知识点:循环语句、条件语句)
1 public class sjlxa { 2 3 public static void main(String[] args) { 4 // TODO Auto-generated method stub 5 6 int x ; 7 int y ; 8 int z ; 9 for (int n=100;n<1000;n++){ 10 11 x = n / 100; 12 y = (n % 100)/10 ; 13 z = n % 10; 14 if(n == x*x*x + y*y*y +z*z*z) 15 { 16 System.out.println(n+"是水仙花数"); 17 } 18 } 19 20 } 21 22 }
2.在控制台输出以下图形(知识点:循环语句、条件语句)
1 public class sjlxb { 2 3 public static void main(String[] args) { 4 // TODO Auto-generated method stub 5 for (int i = 1; i <= 6; i++) { 6 for (int j = 1; j <=i; j++) { 7 System.out.print(j+""); 8 9 } 10 System.out.println( ); 11 12 } 13 14 } 15 16 }
1 public class sjlxb { 2 3 public static void main(String[] args) { 4 // TODO Auto-generated method stub 5 for (int i = 6; i >= 0; i--) { 6 for (int j = 1; j <=i; j++) { 7 System.out.print(j+""); 8 9 } 10 System.out.println( ); 11 12 } 13 14 } 15 16 }
1 public class sjlxb { 2 3 public static void main(String[] args) { 4 // TODO Auto-generated method stub 5 for(int i=1;i<7;i++) { 6 for(int j=i;j>0;j--) { 7 System.out.print(j); 8 } 9 System.out.println( ); 10 } 11 12 13 } 14 15 }
1 public class sjlxb { 2 3 public static void main(String[] args) { 4 // TODO Auto-generated method stub 5 for(int i=6;i>0;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 }
3. 输入年月日,判断这是这一年中的第几天(知识点:循环语句、条件语句)
1 import java.util.Scanner; 2 3 public class sjlxc { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 Scanner input = new Scanner(System.in); 8 System.out.println("输入年月日"); 9 int a= input.nextInt(); 10 int b= input.nextInt(); 11 int c= input.nextInt(); 12 int allday = 0; 13 int[] days = {31,28,31,30,31,30,31,31,30,31,30,31}; 14 for(int i = 0;i < b-1;i++){ 15 allday += days[i]; 16 } 17 allday += c; 18 if(((a % 4==0 && a % 100 == 0)|| a % 400 == 0)&&b > 2){ 19 allday++; 20 } 21 System.out.println(b+"月"+c+"是"+a+"年的第"+allday+"天"); 22 } 23 24 }
4.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321(知识点:循环语句、条件语句)
1 import java.util.Scanner; 2 public class sjlxd { 3 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 num=input.nextInt(); 9 while(num!=0){ 10 int a=num%10; 11 System.out.print(a); 12 num=num/10; 13 14 } 15 16 } 17 18 }