第五周作业
1.分别使用for循环, while循环,do循环求1到100之间所有能被3整除的整数的和。(知识点:循环语句)
public class aa { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int a=0; int s=0; while(a<100){ if(a%3==0) s+=a; a++; } System.out.println(s); }

⒉.输出O-9之间的数,但是不包括5。(知识点:条件、循环语句)
3.public class aa { 4. 5. /** 6. * @param args 7. */ 8. public static void main(String[] args) { 9. // TODO Auto-generated method stub 10.int a=0; 11. 12. 13.while(a<10){ 14.if(a!=5) 15. 16.System.out.println(a); 17.a++;

3.编写一个程序,求整数n的阶乘,例如5的阶乘是1*2*3*4*5(知识点:循环语句)
public class aa { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int n=1; int s=1; while(n<5){ n++; s*=n; } System.out.println(s); } }

4.编写一个程序,输入任意学生成绩,如果输入不合法(<0或者>100),提示输入错误,重新输入,直到输入合法程序结束(知识点:循环语句)
public class aa { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); System.out.println("请输入成绩"); int a=input.nextInt(); if(a<0||a>100) System.out.println("成绩不合法"); while(a<0||a>100){ a=input.nextInt(); } System.out.println("成绩是"); } }

5.假设某员工今年的年薪是30000元,年薪的年增长率6%。编写一个Java应用程序计算该员工10年后的年薪,并统计未来10年(从今年算起)总收入。(知识点:循环语句)
public class test5 { public static void main(String[] args) { // TODO Auto-generated method stub double nx = 30000, sum = 30000; int y = 1; for (y = 1; y < 10; y++) { nx += nx * 0.06; sum += nx; } System.out.println("十年后的年薪" + nx); System.out.println("未来十年总收入" + sum); }

作业
1.打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。(知识点:循环语句、条件语句)
public class homework1 { public static void main(String[] args) { // TODO Auto-generated method stub int i = 100; while (i < 1000) { int g = i % 10; int s = i / 10 % 10; int b = i / 100; if (i == g * g * g + s * s * s + b * b * b) System.out.println(i); i++; } } }

2.输入年月日,判断这是这一年中的第几天(知识点:循环语句、条件语句)
import java.util.Scanner; public class homework2 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.println("输入年月日"); int y = input.nextInt(); int m = input.nextInt(); int d = input.nextInt(); int sum = 0, i = 1; for (i = 1; i < m; i++) { switch (i) { case 4: case 6: case 9: case 11: sum += 30; break; case 2: if (y % 2 == 0 && y % 100 == 0 || y % 400 == 0) sum += 29; else sum += 28; break; default: sum += 31; break; } } sum += d; System.out.println("该天是" + y + "的第" + sum + "天"); } }

3.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321(知识点:循环语句、条件语句)
import java.util.Scanner; public class homework3 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.println("请输入一个四位数"); int a = input.nextInt(); while (a < 1000 || a > 10000) { System.out.println("输入的数字不合法,请重新输入"); a = input.nextInt(); } int g = a % 10; int s = a % 100 / 10; int b = a % 1000 / 100; int q = a / 1000; System.out.println("反转后是" + g + s + b + q); } }


浙公网安备 33010602011771号