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