1. 打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。(知识点:循环语句、条件语句)
1 package as; 2 3 public class unll { 4 public static void main(String[] args) { 5 // TODO Auto-generated method stub 6 int x=100,a,b,c; 7 for(x=100;x<1000;x++){ 8 a=x/100; 9 b=(x%100)/10; 10 c=x%10; 11 if(x==a*a*a+b*b*b+c*c*c){ 12 System.out.println("水仙花数"+x); 13 } 14 } 15 } 16 }
2.在控制台输出以下图形(知识点:循环语句、条件语句)
1 package as; 2 3 public class unll { 4 public static void main(String[] args) { 5 // TODO Auto-generated method stub 6 for(int i=1;i<7;i++) { 7 for(int j=1;j<=i;j++) { 8 System.out.print(j); 9 } 10 System.out.println(); 11 } 12 } 13 }
1 package as; 2 3 public class unll { 4 public static void main(String[] args) { 5 // TODO Auto-generated method stub 6 for(int i=1;i<7;i++) { 7 for(int j=1;j<=7-i;j++) { 8 System.out.print(j); 9 } 10 System.out.println(); 11 } 12 } 13 }
1 package as; 2 3 public class unll { 4 public static void main(String[] args) { 5 // TODO Auto-generated method stub 6 for(int a = 1;a <= 6;a++) 7 { 8 for(int i = 1;i <= 2 * (6 - a);i++) 9 System.out.print(" "); 10 for(int i = a;i >= 1;i--) 11 System.out.printf("%d ",i); 12 System.out.print(" "); 13 } 14 System.out.print(" "); 15 } 16 }
1 package as; 2 3 public class unll { 4 public static void main(String[] args) { 5 // TODO Auto-generated method stub 6 for(int a = 6;a >= 1;a--) 7 { 8 for(int i = 1;i <= 2 *(6-a);i++) 9 System.out.print(" "); 10 for(int i = 1;i <= a;i++) 11 System.out.printf("%d ",i); 12 System.out.print(" "); 13 } 14 } 15 }
3. 输入年月日,判断这是这一年中的第几天(知识点:循环语句、条件语句)
1 package as; 2 import java.util.Scanner; 3 public class unll { 4 public static void main(String[] args) { 5 // TODO Auto-generated method stub 6 Scanner scan = new Scanner(System.in); 7 int year, month, day; 8 System.out.println("请输入年份:"); 9 year = scan.nextInt(); 10 System.out.println("请输入月份:"); 11 month = scan.nextInt(); 12 System.out.println("请输入日期:"); 13 day = scan.nextInt(); 14 switch (month-1) { 15 case 11: 16 day += 30; 17 case 10: 18 day += 31; 19 case 9: 20 day += 30; 21 case 8: 22 day += 31; 23 case 7: 24 day += 31; 25 case 6: 26 day += 30; 27 case 5: 28 day += 31; 29 case 4: 30 day += 30; 31 case 3: 32 day += 31; 33 case 2: 34 if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) { 35 day += 29; 36 } else { 37 day += 28; 38 } 39 case 1: 40 day += 31;break; 41 default: 42 System.out.println("请输入正确月份"); 43 break; 44 } 45 if(month >= 1 && month <= 12){ 46 System.out.println("今天是" + year + "年的第" + day + "天"); 47 } 48 } 49 }
4.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321(知识点:循环语句、条件语句)
1 package as; 2 import java.util.Scanner; 3 public class unll { 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 n=sc.nextInt(); 9 int a = n / 1000; 10 int b = n / 100 % 10; 11 int c = n / 10 % 10; 12 int d = n % 10; 13 int s = d * 1000 + c * 100 + b * 10 + a; 14 System.out.println("反转后数为�" + s); 15 } 16 }