- 打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。(知识点:循环语句、条件语句)
package test; public class finish { public static void main(String[] args) { // TODO Auto-generated method stub int i=100,g,s,b; while(i<=999){ g=i%10; s=i%100/10; b=i/100; if(g*g*g+s*s*s+b*b*b==i){ System.out.println(i); } i++; } } }
2.在控制台输出以下图形(知识点:循环语句、条件语句)
(1) package test1; public class finish { public static void main(String[] args) { // TODO Auto-generated method stub int i,j; for(i=1;i<=6;i++){ for(j=1;j<=i;j++){ System.out.print(j); } System.out.println(""); } } }
package test1; public class finish { public static void main(String[] args) { // TODO Auto-generated method stub int i,j; for(i=6;i>=1;i--){ for(j=1;j<=i;j++){ System.out.print(j); } System.out.println(); } } }
package test1; public class finish { public static void main(String[] args) { // TODO Auto-generated method stub int i,j; for(i=1;i<=6;i++) { for(int m=0;m<6-i;m++) { System.out.print(" "); } for(j=i;j>0;j--) { System.out.print(j); } System.out.println(""); } } }
package test1; public class finish { public static void main(String[] args) { // TODO Auto-generated method stub int i,j; for(i=6;i>0;i--) { for(int k=0;k<6-i;k++) { System.out.print(" "); } for(j=1;j<=i;j++) { System.out.print(j); } System.out.println(""); } } }
3.输入年月日,判断这是这一年中的第几天(知识点:循环语句、条件语句)
package test1; import java.util.*; public class finish { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); System.out.println("请输入年:"); int year=input.nextInt(); System.out.println("请输入月:"); int month=input.nextInt(); System.out.println("请输入日:"); int day=input.nextInt(); int total=0;//统计输入月份之前一共多少天 for(int i=1;i<month;i++) { switch(i){ case 4: case 6: case 9: case 11: total+=day; break; case 2: if(year%4==0&&year%100!=0||year%400==0) total+=29; else total+=28; break; default: total+=31; break; } } total+=31; System.out.println("该天是第"+total+"天"); } }
4.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321(知识点:循环语句、条件语句)
package test1; import java.util.Scanner; public class finish { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); System.out.println("请输入一个四位数"); int i=sc.nextInt(); int fz; if(i>999&&i<=9999) { fz=i/1000+i/100%10*10+i/10%10*100+i%10*1000; System.out.print(fz); }else { System.out.print("错误!"); } } }