第三次上机
1. 打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。(知识点:循环语句、条件语句)
package huaerzi; import java.util.Scanner; public class huada { public static void main(String[] args) { // TODO Auto-generated method stub for(int n=100;n<=999;n++){ int Bai=n/100; int Shi=(n%100)/10; int Ge=n%10; if((Bai*Bai*Bai)+(Shi*Shi*Shi)+(Ge*Ge*Ge)==n) { System.out.println(n); } } } }
2.在控制台输出以下图形(知识点:循环语句、条件语句)
package huaerzi; public class huada { 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 huaerzi; public class huada { 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(); } } }
package huaerzi; public class huada { public static void main(String[] args) { for(int i=1;i<=6;i++) { for(int j=i;j>0;j--) { System.out.print(j); } System.out.println(); } } }
package huaerzi; public class huada { 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 huaerzi; import java.util.Scanner; public class huada { public static void main(String[] args) { // TODO Auto-generated method stub int year; int mouth; int day = 0; int days; int x = 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;} x += day;} System.out.println("第" + year + "年的第" + (x+ days) + "天"); } }
4.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321(知识点:循环语句、条件语句)
package huaerzi; import java.util.Scanner; public class huada { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int x = sc.nextInt(); if(x>999 && x<=9999){ int ge = x%10; int shi = x % 100 / 10; int bai = x%1000/100; int qian = x/1000; int sum = qian + bai*10 +shi*100 +ge*1000; System.out.println(sum); } } }