1.打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。(知识点:循环语句、条件语句)
package xx; public class pika { public static void main(String[] args) { int g,s,b,sum=0; for(int i = 100;i<=999;i++) { g=i%10; s=i/10%10; b=i/100; sum=g*g*g+s*s*s+b*b*b; if(sum==i) { System.out.print(i+" "); } } } }
2.在控制台输出以下图形(知识点:循环语句、条件语句)
package xx; import java.util.Scanner; public class pika { 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(); } } }
package xx; import java.util.Scanner; public class pika { public static void main(String[] args) { for(int i = 1; i <= 6; i++) { for(int j = 1; j <= 7-i; j++) { System.out.print(j+" "); } System.out.println(); } } }
package xx; import java.util.Scanner; public class pika { public static void main(String[] args) { for(int i = 1; i <= 6; i++) { for(int j = i; j >= 1; j--) { System.out.print(j+" "); } System.out.println(); } } }
package xx; public class pika { public static void main(String[] args) { for (int a = 6; a >= 1; a--) { for (int i = 1; i <= 7 - a; i++) { System.out.print(" "); } for (int j = 1; j <= a; j++) { System.out.print(j); } System.out.println(" "); } } }
3. 输入年月日,判断这是这一年中的第几天(知识点:循环语句、条件语句)
package xx; import java.util.Scanner; public class pika { public static void main(String[] args) { int day=0; int month=0; int year=0; int sum=0; int leap; System.out.print("请输入年,月,日\n"); Scanner input = new Scanner(System.in); year=input.nextInt(); month=input.nextInt(); day=input.nextInt(); switch(month) { 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; default: System.out.println("data error");break; } sum=sum+day; if(year%400==0||(year%4==0&&year%100!=0)) leap=1; else leap=0; if(leap==1 && month>2) sum++; System.out.println("It is the the day:"+sum); } }
4.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321(知识点:循环语句、条件语句)
package xx; import java.util.Scanner; public class pika { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); if(a>999 && a<=9999){ int gewei = a%10; int shiwei = a % 100 / 10; int baiwei = a%1000/100; int qianwei = a/1000; int sum = qianwei + baiwei*10 +shiwei*100 +gewei*1000; System.out.println(sum); } else{ System.out.println("error"); } } }
原文:https://www.cnblogs.com/x20425535/p/12618640.html