1. 打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。(知识点:循环语句、条件语句)
package book1; public class five01 { public static void main(String[] args) { // TODO Auto-generated method stub for(int i=100;i<1000;i++) { int ge=i%10; int shi=i/10%10; int bai=i/100; if(ge*ge*ge+shi*shi*shi+bai*bai*bai==i) { System.out.println(i); } } } }
2.在控制台输出以下图形(知识点:循环语句、条件语句)
package book1; public class asd { 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 book1; public class asd { 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 book1; public class asd { public static void main(String[] args) { // TODO Auto-generated method stub for(int i=1;i<=6;i++) { for(int j=i;j>0;j--) { System.out.print(j); } System.out.println(" "); } } }
package book1; public class asd { 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 qwe; import java.util.*; public class test4 { public static void main(String[] args) { // TODO Auto-generated method stub int day = 0; int month = 0; int year = 0; int sum = 0; int a; Scanner input = new Scanner(System.in); System.out.println("请输入年:"); year = input.nextInt(); System.out.println("请输入月:"); month = input.nextInt(); System.out.println("请输入日:"); day = input.nextInt(); switch (month) { case 1: sum = 0; break; case 2: sum = 31; break; case 3: sum = 59; break; case 4: sum = 90; break; case 5: sum = 120; break; case 6: sum = 151; break; case 7: sum = 181; break; case 8: sum = 212; break; case 9: sum = 243; break; case 10: sum = 273; break; case 11: sum = 304; break; case 12: sum = 334; break; default: System.out.println("请输入正确的年,月,日!"); break; } sum = sum + day; if (month >= 0 && month <= 12 && day >= 1 && day <= 31) { if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0) { a = 1; } else { a = 0; } if (a == 1 && month > 2) { sum++; System.out.println(year + "年" + month + "月" + day + "号是这一年的第" + sum + "天。"); } else { System.out.println(year + "年" + month + "月" + day + "号是这一年的第" + sum + "天。"); } } } }
4.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321(知识点:循环语句、条件语句)
package book1; import java.util.Scanner; public class five04 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入一个四位数"); int n = sc.nextInt(); int d = n / 1000; int c = n / 100 % 10; int b = n / 10 % 10; int a = n % 10; int s = a * 1000 + b * 100 + c* 10 + d; System.out.println("反转后数为" + s); } }