1. 打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。(知识点:循环语句、条件语句) package diwuzhou; public class Shuixianhuashu { public static void main(String[] args) { // TODO Auto-generated method stub int i = 100; int ge; int shi; int bai; while (i < 1000) { ge = i % 100 % 10; shi = i / 10 % 10; bai = i / 100; if (ge * ge * ge + shi * shi * shi + bai * bai * bai == i) { System.out.println(i + "是一个水仙花数"); } i++; } } }
2.在控制台输出以下图形(知识点:循环语句、条件语句) (1) package diwuzhou; public class Tuxing { public static void main(String[] args) { // TODO Auto-generated method stub for (int i = 1; i <= 6; i++){ for(int j=1;j<=i;j++){ System.out.print(j); } System.out.println(); } } } (2) package diwuzhou; public class Tuxing2 { public static void main(String[] args) { // TODO Auto-generated method stub for(int i=6;i>=1;i--){ for(int j=1;j<=i;j++){ System.out.print(j); } System.out.println(); } } } (3) package diwuzhou; public class Tuxing3 { public static void main(String[] args) { // TODO Auto-generated method stub for(int i = 1;i <= 6;i++) { for(int j = 1;j <= 2 * (6 - i);j++) System.out.print(" "); for(int j = i;j >= 1;j--) System.out.printf(" %d",j); System.out.println(); } System.out.println(); } } (4) package diwuzhou; public class Tuxing4 { public static void main(String[] args) { // TODO Auto-generated method stub 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 diwuzhou; import java.util.*; public class Shurunianyuefen { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.println("year"); int year = input.nextInt(); System.out.println("month"); int month = input.nextInt(); System.out.println("day"); 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 += 30; break; case 2: if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) total += 29; else total += 28; break; default: total += 31; break; } } total += day; System.out.println("今天是第" + total + "天"); } }
4.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321(知识点:循环语句、条件语句) package diwuzhou; import java.util.*; public class Fanzhuan { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("请输入四位数整数"); Scanner input=new Scanner(System.in); int x=input.nextInt(); int qian=x/1000%10; int bai=x/100%10; int shi=x/10%10; int ge=x%10; int sum=ge*1000+shi*100+bai*10+qian; if(x<1000||x>9999) { System.out.println("输入格式错误"); }else System.out.println(sum); } }