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