1. 打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。(知识点:循环语句、条件语句)
package four; public class text { public static void main(String[] args) { for(int i=100;i<1000;i++) { int ge=i%10; int shi=i/10%10; int bai=i/100; if(ge*ge*ge+shi*shi*shi+bai*bai*bai==i) { System.out.println(i); } } } }
2.在控制台输出以下图形(知识点:循环语句、条件语句)
package four; public class text { public static void main(String[] args) { for(int i=1;i<7;i++) { for(int j=1;j<=i;j++) { System.out.print(j); } System.out.println(); } } }
package four; public class text { public static void main(String[] args) { for(int i=1;i<7;i++) { for(int j=1;j<=7-i;j++) { System.out.print(j); } System.out.println(); } } }
package four; public class text { public static void main(String[] args) { for(int i=1;i<7;i++) { for(int m=1;m<=6-i;m++) { System.out.print(" "); } for(int j=i;j>0;j--) { System.out.print(j); } System.out.println(); } } }
package four; public class text { public static void main(String[] args) { for(int i=6;i>0;i--) { for(int k=0;k<6-i;k++) { System.out.print(" "); } for(int j=1;j<=i;j++) { System.out.print(j); } System.out.println(""); } } }
3. 输入年月日,判断这是这一年中的第几天(知识点:循环语句、条件语句)
package four; import java.util.Scanner; public class text { public static void main(String[] args) { int x = 0; Scanner sc=new Scanner(System.in); System.out.print("请输入年份"); int year=sc.nextInt(); System.out.print("请输入月份"); int month=sc.nextInt(); System.out.print("请输入日期"); int date= sc.nextInt(); if(year%4==0 && year%100!=0 || year%400==0) { switch(month) { case 1: x=0; break; case 2: x=31;break; case 3: x=29+31;break; case 4: x=29+31+31;break; case 5: x=29+31+30+31;break; case 6: x=29+31+30+31+30;break; case 7: x=29+31+30+31+30+31;break; case 8: x=29+31+30+31+30+31+31;break; case 9: x=29+31+30+31+30+31+31+30;break; case 10: x=29+31+30+31+30+31+31+30+31;break; case 11: x=29+31+30+31+30+31+31+30+31+30;break; case 12: x=29+31+30+31+30+31+31+30+31+30+31;break; } }else { switch(month) { case 1: x=0; break; case 2: x=31;break; case 3: x=28+31;break; case 4: x=28+31+31;break; case 5: x=28+31+30+31;break; case 6: x=28+31+30+31+30;break; case 7: x=28+31+30+31+30+31;break; case 8: x=28+31+30+31+30+31+31;break; case 9: x=28+31+30+31+30+31+31+30;break; case 10: x=28+31+30+31+30+31+31+30+31;break; case 11: x=28+31+30+31+30+31+31+30+31+30;break; case 12: x=28+31+30+31+30+31+31+30+31+30+31;break; } } System.out.println(year+"年"+month+"月"+date+"日"+"是第"+(x+date)+"天"); } }
4.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321(知识点:循环语句、条件语句)
package four; import java.util.Scanner; public class text { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("请输入一个四位数"); int num=sc.nextInt(); int qian=num/1000; int bai=num/100%10; int shi=num/10%10; int ge=num%10; System.out.println("原先的数为:"+num+"现在的为"+(qian+bai*10+shi*100+ge*1000)); } }