java第五次作业
1.分别使用for循环,while循环,do循环求1到100之间所有能被3整除的整数的和。
package School.Day6; public class Test03 { public static void main(String[] args){ int i = 1; int sum = 0; for( i = 1;i<=100;i++){ if(i%3 == 0){ sum = sum + i; } } System.out.println(sum); i = 1; sum = 0; while(i<=100){ if(i%3 == 0){ sum = sum + i; } i++; } System.out.println(sum); sum = 0; i = 1; do{ if(i % 3 == 0){ sum = sum + i; } i++; }while(i<=100); System.out.println(sum); } }
2.输出0-9之间的数,但是不包括5。
package School.Day6; public class Test04 { public static void main(String[] args) { for (int i = 0; i <= 9; i++) { if (i != 5 ) { System.out.println(i); } } } }
3.编写一个程序,求整数n的阶乘
package School.Day6; import java.util.Scanner; public class Test05 { public static void main(String[] args) { System.out.print("输入一个数: "); Scanner s = new Scanner(System.in); int x = s.nextInt(); int sum = 1; for (int i = x; i > 0; i--) { sum = sum * i; } System.out.println(x + "的阶乘是" +sum); } }
4.编写一个程序,输入任意学生成绩,如果输入不合法(<0或者>100),提示输入错误,重新输入,直到输入合法程序结束
package School.Day6; import java.util.Scanner; public class Test06 { public static void main(String[] args){ Scanner s = new Scanner(System.in); System.out.print("输入成绩: "); double x = s.nextDouble(); while(true){ if(x>0 && x<100){ System.out.println("输入成功!"); return; }else{ System.out.println("成绩不合法"); System.out.print("输入成绩: "); x = s.nextDouble(); } } } }
5.假设某员工今年的年薪是30000元,年薪的年增长率6%。编写一个Java应用程序计算该员工10年后的年薪,并统计未来10年(从今年算起)总收入。
package School.Day6; public class Test07 { public static void main(String[] args){ double gongzi = 30000.00; double heji = 0; for(int i = 1 ;i<=9;i++){ gongzi = gongzi + (gongzi * 0.06); heji = heji + gongzi; }
heji = heji + 30000; System.out.println("十年后工资是: " + gongzi); System.out.println("这十年工资合计是: " + heji); } }
1.打印出所有的"水仙花数"
package School.Day6; public class Test02 { public static void main(String[] args) { for (int i = 100; i < 1000; i++) { int a = i/100; int b = i%100/10; int c = i%10; int a3 = a * a * a; int b3 = b * b * b; int c3 = c * c * c; if (a3 + b3 + c3 ==i) { System.out.println("水仙花: " + i); } } } }
2.输入年月日,判断这是这一年中的第几天
package School.Day6; import java.util.Scanner; public class Test01 { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("输入年份: "); int y = s.nextInt(); System.out.print("输入月份: "); int m = s.nextInt(); System.out.print("输入日: "); int d = s.nextInt(); int tianshu = 0; switch(m){ case 12: tianshu = tianshu + 30; case 11: tianshu = tianshu + 31; case 10: tianshu = tianshu + 30; case 9 : tianshu = tianshu + 31; case 8 : tianshu = tianshu + 31; case 7 : tianshu = tianshu + 30; case 6 : tianshu = tianshu + 31; case 5 : tianshu = tianshu + 30; case 4 : tianshu = tianshu + 31; case 3 : if (y%4==0 && y%100 !=0 || y%400 ==0) { tianshu = tianshu + 29; }else{ tianshu = tianshu + 28; } case 2: if (y%4==0 && y%100 !=0 || y%400 ==0) { if (d > 29 && m == 2) { System.out.println("2月最多29天!"); return; } }else{ if (d > 28 && m ==2) { System.out.println("平年2月只有28天!"); return; } } tianshu = tianshu + 31; default: break; } tianshu = tianshu + d; System.out.println(y + "年" + m + "月" + d + "日是这一年的第" + tianshu +"天"); } }
3.由控制台输入一个4位整数,求将该数反转以后的数
package School.Day6; import java.util.Scanner; public class Test08 { public static void main(String[] args) { Scanner c = new Scanner(System.in); System.out.print("输入四位数: "); int a = c.nextInt(); if (a > 9999 || a < 1000 ) { System.out.println("数字错误!"); System.exit(0); } int s = a / 1000; int h = a % 1000 /100; int m = a % 100 /10; int g = a % 10; System.out.print(a + "的反转数为: " ); System.out.print(g); System.out.print(m); System.out.print(h); System.out.print(s); } }