java第五次作业
1.分别使用for循环,while循环,do循环求1到100之间所有能被3整除的整数的和。(知识点:循环语句)
package DaySix; public class Test1 { public static void main(String[] args) { for (int i = 1; i < 100; i++) { if(i%3==0){ System.out.print(i+" "); } } } } package DaySix; public class Test1 { public static void main(String[] args) { int i = 1; while(i<100){ i++; if(i%3==0){ System.out.print(i+" "); } } } } package DaySix; public class Test1 { public static void main(String[] args) { int i = 1; do { i++; if (i % 3 == 0) { System.out.print(i + " "); } } while (i < 100); } }
2.输出0-9之间的数,但是不包括5。(知识点:条件、循环语句)
package DaySix; public class Test1 { public static void main(String[] args) { int i = 0; do { if (i != 5) { System.out.print(i + " "); } i++; } while (i <= 9); } }
3.编写一个程序,求整数n的阶乘,例如5的阶乘是1*2*3*4*5(知识点:循环语句)
package DaySix; import java.util.Scanner; public class Test2 { public static void main(String[] args) { System.out.println("输入一个整数"); Scanner sc = new Scanner(System.in); int num = sc.nextInt(); int chengji = 1; for (int i = 1; i <= num; i++) { chengji = chengji*i; } System.out.println(chengji); } }
4.编写一个程序,输入任意学生成绩,如果输入不合法(<0或者>100),提示输入错误,重新输入,直到输入合法程序结束(知识点:循环语句)
package DaySix; import java.util.Scanner; public class Test3 { public static void main(String[] args) { System.out.println("输入学生成绩"); Scanner sc = new Scanner(System.in); double score = sc.nextDouble(); boolean flag =false; if(score<0||score>100){ flag = true; }else{ System.out.println(score); } while(flag){ System.out.println("你输入的成绩有误,输入正确的学生成绩"); double newscore = sc.nextDouble(); if(newscore>0||newscore<100){ System.out.println(newscore); break; } } } }
5.假设某员工今年的年薪是30000元,年薪的年增长率6%。编写一个Java应用程序计算该员工10年后的年薪,并统计未来10年(从今年算起)总收入。(知识点:循环语句)
package DaySix; public class Test4 { public static void main(String[] args) { double nianxin = 30000; double sum = 0; for (int i = 1; i <= 10; i++) { double newxin = nianxin * (1 + 0.06*i); if(i == 10){ System.out.println("第十年的薪水:"+newxin); } sum = newxin + sum; } System.out.println("总收入:"+sum); } }
1.打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。(知识点:循环语句、条件语句)
package homework; public class tets25 { 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 homework; import java.util.Scanner; public class test26 { 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; } } }
3.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321(知识点:循环语句、条件语句)
package homework; import java.util.Scanner; public class tets27 { 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); } } }
posted on 2021-04-06 21:14 chenyulin11 阅读(45) 评论(0) 编辑 收藏 举报