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