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