Java第六次作业
1.打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。(知识点:循环语句、条件语句)
public static void main(String[] args) { for (int i = 2; i < 1000; i++) { int a = i / 100; int b = i % 100 / 10; int c = i % 10; if (a * a * a + b * b * b + c * c * c == i) { System.out.println(i); } } }
2.在控制台输出以下图形(知识点:循环语句、条件语句)

public static void main(String[] args) { for (int i = 0; i <= 6; i++) { for (int j = 1; j <= i; j++) { System.out.print(" " + j); } System.out.println(); } for (int i = 6; i > 0; i--) { for (int j = 1; j <= i; j++) { System.out.print(" " + j); } System.out.println(); } for (int i = 0; i <= 6; i++) { for (int j = 6 - i; j > 0; j--) { System.out.print(" " + j); } System.out.println(); } }
3.输入年月日,判断这是这一年中的第几天(知识点:循环语句、条件语句)
public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("请输入年份"); int year = input.nextInt(); System.out.println("请输入月份"); int month = input.nextInt(); System.out.println("请输入日期"); int day = input.nextInt(); int total = 0; switch (month) { case 2: if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { total += 29; } else { total += 28; } break; case 1: case 3: case 5: case 7: case 8: case 10: case 12: total += 31; break; default: total += 30; break; } total += day; System.out.println("这是这一年中的第" + total + "天"); }
4.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321(知识点:循环语句、条件语句)
public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("请输入一个四位整数"); int i = input.nextInt(); if (i >= 10000 || i < 1000) { System.out.println("输入有误"); } else { int a = i / 1000; int b = i % 1000 / 100; int c = i % 100 / 10; int d = i % 10; System.out.print("原数为:" + i + " " + "反转后为:" + d + c + b + a); } }





浙公网安备 33010602011771号