Java第五次作业
1.求1到100之间所有能被3整除的整数的和(while;do;for)
public static void main(String[] args) { int a=1; while(a<=100){ if(a%3==0){ System.out.println(a); } a++; } // TODO Auto-generated method stub }
public static void main(String[] args) { int a ; for(a=1;a<=100;a++){ if(a%3==0){ System.out.println(a); } } // TODO Auto-generated method stub }
public static void main(String[] args) { int a=1 ; do { if (a%3==0) { System.out.println(a); } a++; } while (a<=100); // TODO Auto-generated method stub }
2.输出0-9之间的数,但是不包括5.
public static void main(String[] args) { //输出0-9之间的数,但是不包括5. int a=0 ; do { if (a!=5) { System.out.println(a); } a++; } while (a<=9); // TODO Auto-generated method stub }
3.编写一个程序,求整数n的阶乘,例如5的阶乘是1*2*3*4*5.
public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); System.out.println("输入n的值求其阶乘"); int n=input.nextInt(); int cheng=1; for(int i=1;i<=n;i++) cheng*=i; System.out.println(cheng); }
4.编写一个程序,输入任意学生成绩,如果输入不合法(<0或者>100),提示输入错误,重新输入,直到输入合法程序结束
public static void main(String[] args) { // TODO Auto-generated method stub while(true){ Scanner sc=new Scanner(System.in); System.out.println("输入学生成绩"); int score = sc.nextInt(); if(score>0&&score<=100) {System.out.println("输入的成绩有效"); break;} else System.out.println("输入的成绩无效,请重新输入"); } }
5.假设某员工今年的年薪是30000元,年薪的年增长率6%。编写一个Java应用程序计算该员工10年后的年薪,并统计未来10年(从今年算起)总收入
public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("十年后的变化"); double nianxin=30000; double sum=0; for(int i=1;i<=10;i++) if(i!=1) {nianxin*=1.06; sum+=nianxin; } System.out.println("十年后年薪为"+nianxin); System.out.println("十年总合为"+sum); }
6.打印所有的水仙花数
public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("输出1000之内的水仙花数"); int i,baiwei,shiwei,gewei; for(i=100;i<1000;i++){ baiwei=i/100; shiwei=i/10%10; gewei=i%10; if(i==baiwei*baiwei*baiwei+shiwei*shiwei*shiwei+gewei*gewei*gewei){ System.out.println(i+"是水仙花数"); } } }
7.输入年月日判断这是一年的第几天
public static void main(String[] args) { Scanner sc = new Scanner(System.in); int year = sc.nextInt(); int mouth = sc.nextInt(); int day = sc.nextInt(); if ((year % 4 == 0 && year % 100 != 0 || year % 400 == 0) && mouth > 2) switch (mouth) { case 1: System.out.println("该天为这一年的" + day + "天"); break; case 2: System.out.println("该天为这一年的" + day + 31 + 1 + "天"); break; case 3: System.out.println("该天为这一年的" + day + 59 + 1 + "天"); break; case 4: System.out.println("该天为这一年的" + day + 90 + 1 + "天"); break; case 5: System.out.println("该天为这一年的" + day + 120 + 1 + "天"); break; case 6: System.out.println("该天为这一年的" + day + 151 + 1 + "天"); break; case 7: System.out.println("该天为这一年的" + day + 181 + 1 + "天"); break; case 8: System.out.println("该天为这一年的" + day + 212 + 1 + "天"); break; case 9: System.out.println("该天为这一年的" + (day + 243 + 1) + "天"); break; case 10: System.out.println("该天为这一年的" + day + 273 + 1 + "天"); break; case 11: System.out.println("该天为这一年的" + day + 304 + 1 + "天"); break; case 12: System.out.println("该天为这一年的" + day + 334 + 1 + "天"); break; } else switch (mouth) { case 1: System.out.println("该天为这一年的" + day + "天"); break; case 2: System.out.println("该天为这一年的" + day + 31 + "天"); break; case 3: System.out.println("该天为这一年的" + day + 59 + "天"); break; case 4: System.out.println("该天为这一年的" + day + 90 + "天"); break; case 5: System.out.println("该天为这一年的" + day + 120 + "天"); break; case 6: System.out.println("该天为这一年的" + day + 151 + "天"); break; case 7: System.out.println("该天为这一年的" + day + 181 + "天"); break; case 8: System.out.println("该天为这一年的" + day + 212 + "天"); break; case 9: System.out.println("该天为这一年的" + day + 243 + "天"); break; case 10: System.out.println("该天为这一年的" + day + 273 + "天"); break; case 11: System.out.println("该天为这一年的" + day + 304 + "天"); break; case 12: System.out.println("该天为这一年的" + day + 334 + "天"); break; } } }
8.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321(知识点:循环语句、条件语句)
public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入一个整数:"); int a = sc.nextInt(); while (a > 0) { int b = a % 10; a = a / 10; System.out.print(b); } }