第5次JAVA
1.打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。(知识点:循环语句、条件语句)
import java.util.*; public class jkk { public static void main(String[] args) { 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.在控制台输出以下图形(知识点:循环语句、条件语句)
import java.util.*; public class jkk { public static void main(String[] args) { for(int i=1;i<7;i++) { for(int j=1;j<=i;j++) { System.out.print(j); } System.out.println(); } } }
import java.util.*; public class jkk { 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(); } } }
import java.util.*; public class jkk { public static void main(String[] args) { for(int i=1;i<7;i++) { for(int j=i;j>0;j--) { System.out.print(j); } System.out.println(); } } }
import java.util.*; public class jkk { 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.输入年月日,判断这是这一年中的第几天(知识点:循环语句、条件语句)
import java.util.*; public class jkk { public static void main(String[] args) { Scanner sr = new Scanner(System.in); System.out.println("输入年份"); int nian = sr.nextInt(); System.out.println("输入月份"); int yue = sr.nextInt(); System.out.println("输入日"); int ri = sr.nextInt(); int sum = 0; int flag = 0; switch (yue) { 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; } sum = sum + ri; if (nian % 4 == 0 && nian % 100 == 0) { flag = 1; } else { flag = 0; } if (yue > 2) { sum = sum + ri + 1; } System.out.println("这是" + nian + "的第" + sum + "天"); } }
4.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321(知识点:循环语句、条件语句)
import java.util.*; public class jkk { public static void main(String[] args) { System.out.print("请输入数字\n"); 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); } else { System.out.println("wrong"); } } }