1. 打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。(知识点:循环语句、条件语句)
public class a1 { 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); } } } }
2.在控制台输出以下图形(知识点:循环语句、条件语句)
public class a1 { 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(); } } }
public class a1 { 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(); } } }
public class a1 { 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(); } } }
public class a1 { 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(); } } }
3. 输入年月日,判断这是这一年中的第几天(知识点:循环语句、条件语句)
import java.util.Scanner; public class a1 { 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+" 天"); } } }
import java.util.Scanner; public class a1 { 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.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321(知识点:循环语句、条件语句)
import java.util.Scanner; public class a1 { 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); } }