第五周上机
1.打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。(知识点:循环语句、条件语句)
package sat; public class bat { public static void main(String[] args) { // TODO Auto-generated method stub int i = 100; int ge, shi, bai; for (i = 100; i < 1000; i++) { ge = i % 10; shi = i / 10 % 10; bai = i / 100; if (i == ge * ge * ge + shi * shi * shi + bai * bai * bai) { System.out.println("水仙花数为" + i); } } } }
2.在控制台输出以下图形(知识点:循环语句、条件语句)
(1) package sat; import java.util.*; public class bat { public static void main(String[] args) { // TODO Auto-generated method stub for (int i = 1; i <= 6; i++) { for (int j = 1; j <= i; j++) { System.out.print(j); } System.out.println(); } } }
(2) package sat; import java.util.*; public class bat { public static void main(String[] args) { // TODO Auto-generated method stub for (int i = 6; i >=0 ; i--) { for (int j = 1; j <= i; j++) { System.out.print(j); } System.out.println(); } } }
(3) package sat; import java.util.*; public class bat { public static void main(String[] args) { // TODO Auto-generated method stub for (int i = 1; i < 7; i++) { for (int j = i; j > 0; j--) { System.out.print(j); } System.out.println(); } } }
(4) package sat; import java.util.*; public class bat { public static void main(String[] args) { // TODO Auto-generated method stub for (int i = 6; i > 0; i--) { for (int j = 0; j < 6 - i; j++) { System.out.print(" "); } for (int a= 1; a <= i; a++) { System.out.print(a); } System.out.println(""); } } }
3. 输入年月日,判断这是这一年中的第几天(知识点:循环语句、条件语句)
package sat; import java.util.*; public class bat { public static void main(String[] args) { // TODO Auto-generated method stub int y, m, d; int a = 0, b = 0; Scanner sc = new Scanner(System.in); System.out.println("请输入年份"); y = sc.nextInt(); System.out.println("请输入月份"); m = sc.nextInt(); System.out.println("请输入日"); d = sc.nextInt(); for (int i = 1; i < m; i++) { switch (i) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: a = 31; break; case 2: if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) a = 29; else a = 28; break; case 4: case 6: case 9: case 11: a = 30; break; } b = b + a; } b = b + d; System.out.println("这是这一年的第" + b + "天"); } }
4.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321(知识点:循环语句、条件语句)
package sat; import java.util.*; public class bat { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); System.out.println("请输入一个四位数整数:"); int a = sc.nextInt(); int ge, shi, bai, qian; ge = a % 10; shi = a / 10 % 10; bai = a/10/10%10; qian = a / 1000; System.out.println(ge * 1000 + shi * 100 + bai * 10 + qian); } }