java第五次作业
1.分别使用for循环,while循环,do循环求1到100之间所有能被3整除的整数的和。
packagesjlx; publicclass exe { /** * @paramargs */ publicstaticvoid main(String[] args) { // TODO Auto-generated method stub int sum=0; int i=1; while (i<=100){ if (i%3==0) sum+=i; i++; }System.out.println(sum); } }
2.输出0-9之间的数,但是不包括5.
packagesjlx; publicclass exe2 { /** * @paramargs */ publicstaticvoid main(String[] args) { // TODO Auto-generated method stub int i=0; while (i<=9){ if(i==5){ System.out.print(""); }else{ System.out.println(i); }i++; } } }
3.编写一个程序,求整数n的阶乘,例如5的阶乘是1*2*3*4*5
packagesjlx; importjava.util.Scanner; publicclass exe3 { /** * @paramargs */ publicstaticvoid main(String[] args) { // TODO Auto-generated method stub Scanner input = newScanner(System.in); System.out.println("输入一个数"); int n = input.nextInt(); int sum = 1; int i = 1; while (i <= n) { sum *= i; i++; } System.out.println(sum); } }
4.编写一个程序,输入任意学生成绩,如果输入不合法(<0或者>100),提示输入错误,重新输入,直到输入合法程序结束
packagesjlx; importjava.util.Scanner; publicclass exe4 { /** * @paramargs */ publicstaticvoid main(String[] args) { // TODO Auto-generated method stub Scanner input = newScanner(System.in); System.out.println("输入学生成绩"); int i=input.nextInt(); while (i>=100||i<=0){ System.out.println("你的成绩出新错误"); i=input.nextInt(); }System.out.println(i); } }
5.假设某员工今年的年薪是30000元,年薪的年增长率6%。编写一个Java应用程序计算该员工10年后的年薪,并统计未来10年从(今年算起)总收入。
packagesjlx; publicclass exe5 { /** * @paramargs */ publicstaticvoid main(String[] args) { // TODO Auto-generated method stub int i=1; double x=30000; double y=x; while (i <= 9){ x=x*1.06; y=y+x; i++; }System.out.println("十年后的年薪为"+x); System.out.println("这十年的总收入为"+y); } }
1.打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。(知识点:循环语句、条件语句)
package exe2; public class text5 { public static void main(String[] args) { int i = 100; int d, b, c; for (i = 100; i < 1000; i++) { d = i / 100; b = i / 10 % 10; c = i % 10; if (d * d * d + b * b * b + c * c * c == i) { System.out.println(i); } } } }
2.输入年月日,判断这是这一年中的第几天(知识点:循环语句、条件语句)
package exe2; import java.util.Scanner; public class text9 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("输入年份"); int nian = input.nextInt(); System.out.println("输入月份"); int yue = input.nextInt(); System.out.println("输入日期"); int ri = input.nextInt(); if (nian % 400 == 0 || (nian % 4 == 0 && nian % 100 != 0)) { System.out.println("本年二月只有二十八天"); switch (yue) { case 1: System.out.println(ri); break; case 2: System.out.println(31 + ri); break; case 3: System.out.println(31 + 28 + ri); break; case 4: System.out.println(31 + 28 + 31 + ri); break; case 5: System.out.println(31 + 28 + 31 + 30 + ri); break; case 6: System.out.println(31 + 28 + 31 + 30 + 31 + ri); break; case 7: System.out.println(31 + 28 + 31 + 30 + 31 + 30 + ri); break; case 8: System.out.println(31 + 28 + 31 + 30 + 31 + 30 + 31 + ri); break; case 9: System.out.println(31 + 28 + 31 + 30 + 31 + 30 + 31 + ri); break; case 10: System.out.println(31 + 28 + 31 + 30 + 31 + 30 + 31 + 30 + ri); break; case 11: System.out.println(31 + 28 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + ri); break; case 12: System.out.println(31 + 28 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + 30 + ri); break; } } else { System.out.println("本年二月只有二十九天"); switch (yue) { case 1: System.out.println(ri); break; case 2: System.out.println(31 + ri); break; case 3: System.out.println(31 + 29 + ri); break; case 4: System.out.println(31 + 29 + 31 + ri); break; case 5: System.out.println(31 + 29 + 31 + 30 + ri); break; case 6: System.out.println(31 + 29 + 31 + 30 + 31 + ri); break; case 7: System.out.println(31 + 29 + 31 + 30 + 31 + 30 + ri); break; case 8: System.out.println(31 + 29 + 31 + 30 + 31 + 30 + 31 + ri); break; case 9: System.out.println(31 + 29 + 31 + 30 + 31 + 30 + 31 + ri); break; case 10: System.out.println(31 + 29 + 31 + 30 + 31 + 30 + 31 + 30 + ri); break; case 11: System.out.println(31 + 29 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + ri); break; case 12: System.out.println(31 + 29 + 31 + 30 + 31 + 30 + 31 + 30 + 31 + 30 + ri); break; } } } }
3.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321(知识点:循环语句、条件语句)
package exe2; import java.util.Scanner; public class text8 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.println("输入一个四位整数"); int a = input.nextInt(); int b, c, d, e; if (a < 1000 || a > 10000) { System.out.println("输入的数不合法"); } else { b = a / 1000; c = a % 1000 / 100; d = a % 100 / 10; e = a % 10; System.out.println(e * 1000 + d * 100 + c * 10 + b); } } }