第四周作业(2)

1. 分别使用for循环,while循环,do循环求1到100之间所有能被3整除的整数的和。(知识点:循环语句)

 1 package pro1;
 2 import java.util.Scanner;
 3 public class test {
 4     public static void main(String[] args) {
 5         int sum = 0;
 6         int i = 1;
 7         for(;i <= 100; i++){
 8             if(i % 3 == 0){
 9                 sum = sum + i;
10             }
11             else{
12             }
13         }
14         System.out.println(sum);
15     }
16 }
 1 package pro1;
 2 import java.util.Scanner;
 3 public class test {
 4     public static void main(String[] args) {
 5         int sum = 0;
 6         int i = 1;
 7         while(i<=100){
 8             if(i % 3 == 0){
 9                 sum = sum + i;
10                 i++;
11             }
12             else{
13                 i++;
14             }
15         }
16         System.out.println(sum);
17     }
18 }
 1 package pro1;
 2 import java.util.Scanner;
 3 public class test {
 4     public static void main(String[] args) {
 5         int sum    = 0;
 6         int i = 1;
 7         do {
 8             if(i % 3 == 0) {
 9             sum += i;
10             }
11             i++;
12         }while(i<=100);
13         System.out.println(sum);
14     }
15 }

2. 输出0-9之间的数,但是不包括5。(知识点:条件、循环语句)

 1 package pro1;
 2 import java.util.Scanner;
 3 public class test {
 4     public static void main(String[] args) {
 5          for(int i = 1; i <= 9; i++){
 6             if(i != 5){
 7                 System.out.println(i);
 8             }                
 9          }
10     }
11 }

3. 编写一个程序,求整数n的阶乘,例如5的阶乘是1*2*3*4*5(知识点:循环语句)

 1 package pro1;
 2 import java.util.Scanner;
 3 public class test {
 4     public static void main(String[] args) {
 5         int sum = 1;
 6         Scanner aaa = new Scanner(System.in);
 7         System.out.println("请输入一个数:");
 8         int n = aaa.nextInt();
 9         for (int i = 1; i <= n; i++) {
10             sum = sum*i;
11         }
12         System.out.println(n+"阶乘为"+sum);
13     }
14 }

4. 编写一个程序,输入任意学生成绩,如果输入不合法(<0或者>100),提示输入错误,重新输入,直到输入合法程序结束(知识点:循环语句)

 1 package pro1;
 2 import java.util.Scanner;
 3 public class test {
 4     public static void main(String[] args) {
 5         Scanner input = new Scanner(System.in);
 6         int i = 0;
 7         for (i = 0 ; ; i++) {
 8             System.out.print("请输入成绩:");
 9             int score = input.nextInt();
10             if (score < 0 || score > 100) {
11                 System.out.println("输入成绩错误,请重新输入!");
12             } else {
13                 System.out.println("该学生成绩为:" + score);
14             }
15         }
16     }
17 }

5. 假设某员工今年的年薪是30000元,年薪的年增长率6%。编写一个Java应用程序计算该员工10年后的年薪,并统计未来10年(从今年算起)总收入。(知识点:循环语句)

 1 package pro1;
 2 import java.util.Scanner;
 3 public class test {
 4     public static void main(String[] args) {
 5         double a = 30000;
 6         double sum = 30000;
 7         for(int i = 1 ; i <= 10 ; i++ ){
 8             a = a * ( 1 + 0.06 );
 9             sum += a;  
10         }
11         System.out.println("第10年年薪为"+a+"\n未来10年总收入为"+sum);
12     }
13 }

猜你喜欢

转载自www.cnblogs.com/qq1123514689/p/12597355.html