1.打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。(知识点:循环语句、条件语句)
package xx; public class pika { public static void main(String[] args) { int g,s,b,sum=0; for(int i = 100;i<=999;i++) { g=i%10; s=i/10%10; b=i/100; sum=g*g*g+s*s*s+b*b*b; if(sum==i) { System.out.print(i+" "); } } } }
![](https://img2020.cnblogs.com/blog/1965478/202004/1965478-20200402114855394-1660845800.png)
2.在控制台输出以下图形(知识点:循环语句、条件语句)
package xx; import java.util.Scanner; public class pika { 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(); } } }
![](https://img2020.cnblogs.com/blog/1965478/202004/1965478-20200402114954702-2057186924.png)
package xx; import java.util.Scanner; public class pika { public static void main(String[] args) { for(int i = 1; i <= 6; i++) { for(int j = 1; j <= 7-i; j++) { System.out.print(j+" "); } System.out.println(); } } }
![](https://img2020.cnblogs.com/blog/1965478/202004/1965478-20200402115013624-1250532096.png)
package xx; import java.util.Scanner; public class pika { public static void main(String[] args) { for(int i = 1; i <= 6; i++) { for(int j = i; j >= 1; j--) { System.out.print(j+" "); } System.out.println(); } } }
![](https://img2020.cnblogs.com/blog/1965478/202004/1965478-20200402115028625-1056837219.png)
package xx; public class pika { public static void main(String[] args) { for (int a = 6; a >= 1; a--) { for (int i = 1; i <= 7 - a; i++) { System.out.print(" "); } for (int j = 1; j <= a; j++) { System.out.print(j); } System.out.println(" "); } } }
![](https://img2020.cnblogs.com/blog/1965478/202004/1965478-20200402115044457-1234855328.png)
3. 输入年月日,判断这是这一年中的第几天(知识点:循环语句、条件语句)
package li; import java.util.Scanner; public class Test05{ public static void main(String[] args) { int year; int mouth; int day = 0; int days; int d = 0; // 累计天数 int e = 0; Scanner scanner = new Scanner(System.in); //判断输入的年份和月份和日期是否错误 do { System.out.println("输入年:"); year = scanner.nextInt(); System.out.println("输入月:"); mouth = scanner.nextInt(); System.out.println("输入日:"); days = scanner.nextInt(); if (year < 0 || mouth < 0 || mouth > 12 || days < 0 || days > 31) { System.out.println("input error!"); e = 1; } } while (e == 1); for (int i = 1; i < mouth; i++) { switch (i) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: { day = 31; break; } case 4: case 6: case 9: case 11: { day = 30; break; } case 2: { if ((year % 100 != 0 && year % 4 == 0) || (year % 100 == 0 && year % 400 == 0)) { day = 29; } else { day = 28; } } default: break; } d += day; } System.out.println("这是" + year + "年的" + (d + days) + "天"); } }
4.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321
![复制代码](https://common.cnblogs.com/images/copycode.gif)
package li; import java.util.Scanner; public class Test05 { public static void main(String[] args) { int a, b, c, d, e, x; Scanner sc = new Scanner(System.in); System.out.println("输入数字:"); e = sc.nextInt(); a = e / 1000; b = e / 100 % 10; c = e / 10 % 10; d = e % 10; x = d * 1000 + c * 100 + b * 10 + a; System.out.println("反转后数为:" + x); } }
![](https://img2020.cnblogs.com/blog/1965478/202004/1965478-20200402115239846-795953076.png)