1. 分别使用for循环,while循环,do循环求1到100之间所有能被3整除的整数的和。(知识点:循环语句)
package doom3; public class thefourweekworks1_0 { public static void main(String[] args) { // TODO Auto-generated method stub int i = 1; int sum = 0; for (i = 1; i <= 100; i++) { if (i % 3 == 0) sum += i; } System.out.print(sum); } }
package doom3; public class thefourweekworks1_1 { 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+=i; i++; } System.out.print(sum); } }
package doom3; public class thefourweekworks1_2 { public static void main(String[] args) { // TODO Auto-generated method stub int i = 1; int sum = 0; do { if (i % 3 == 0) sum += i; i++; } while (i <= 100); System.out.print(sum); } }
2. 输出0-9之间的数,但是不包括5。(知识点:条件、循环语句)
package doom3; public class thefourweekworks2 { public static void main(String[] args) { // TODO Auto-generated method stub int i=0; for(i=0;i<10;i++){ if(i==5) continue; System.out.print(" "+i+" "); } } }
3. 编写一个程序,求整数n的阶乘,例如5的阶乘是1*2*3*4*5(知识点:循环语句)
package doom3; import java.util.Scanner; public class thefourweekworks3 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.print("请输入一个数字:"); int n = input.nextInt(); int i = 1; int sum = 1; for (i = 1; i <= n; i++) { sum *= i; } System.out.println(sum); } }
4. 编写一个程序,输入任意学生成绩,如果输入不合法(<0或者>100),提示输入错误,重新输入,直到输入合法程序结束(知识点:循环语句)
package doom3; import java.util.Scanner; public class thefourweekworks4 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.print("请输入一名学生的成绩:"); int i = input.nextInt(); while (i < 0 || i > 100) { System.out.print("你输入的成绩有问题,请重新输入:"); i = input.nextInt(); } System.out.print("你输入的同学成绩为:" + i); } }
5. 假设某员工今年的年薪是30000元,年薪的年增长率6%。编写一个Java应用程序计算该员工10年后的年薪,并统计未来10年(从今年算起)总收入。(知识点:循环语句)
package doom3; public class thefourweekworks5 { public static void main(String[] args) { // TODO Auto-generated method stub int i = 0; double n = 30000; double count = n; for (i = 0; i <= 9; i++) { n = n * 1.06; count = count + n; } System.out.println("该员工10年后的年薪:" + n); System.out.println("该员工10年内的总收入:" + count); } }