第四周作业

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

 1 package as;
 2 
 3 public class unll {
 4     public static void main(String[] args) {
 5         // TODO Auto-generated method stub
 6          int sum=0;
 7          for(int i=3;i<=100;i+=3){
 8              sum+=i;
 9              }
10           System.out.println("和为"+sum);
11     }    
12 }
 1 package as;
 2 
 3 public class unll {
 4     public static void main(String[] args) {
 5         // TODO Auto-generated method stub
 6         int sum=0;
 7         int i=1;
 8         while(i<=100){
 9             if(i%3==0){
10               sum+=i;
11             }
12             i++;
13         }
14           System.out.println("和为"+sum);
15     }    
16 }
 1 package as;
 2 
 3 public class unll {
 4     public static void main(String[] args) {
 5         // TODO Auto-generated method stub
 6         int i=1;
 7         int sum=0;
 8         do{
 9             if(i%3==0){
10                 sum=sum+i;
11             }
12                 i++;
13             }while(i<=100);
14           System.out.println("和为"+sum);
15     }    
16 }

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

 1 package as;
 2 
 3 public class unll {
 4     public static void main(String[] args) {
 5         // TODO Auto-generated method stub
 6         System.out.println("0-9之间不包括5的整数:");
 7         for(int i=0;i<=9;i++){
 8            if(i!=5){
 9                System.out.print(i+" ");
10               }
11          }
12       }
13 }    

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

 1 package as;
 2 import java.util.Scanner;
 3 public class unll {
 4     public static void main(String[] args) {
 5         // TODO Auto-generated method stub
 6         Scanner input=new Scanner(System.in);
 7         System.out.println("输入n值");
 8         int n=input.nextInt();
 9         int jc=1;
10         for(int i=1;i<=n;i++){
11              jc*=i;
12               }System.out.println("阶乘为"+jc);
13     }
14 }

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

 1 package as;
 2 import java.util.*;
 3 public class unll {
 4     public static void main(String[] args) {
 5         // TODO Auto-generated method stub
 6         Scanner input=new Scanner(System.in);
 7         System.out.println("请输入学生成绩");
 8         int a=input.nextInt();
 9         while(a<0||a>100){
10             System.out.println("输入错误,请重新输入:");
11               a= input.nextInt();
12         }
13         System.out.println("输入合法");
14     }
15 }

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

 1 package as;
 2 
 3 public class unll {
 4     public static void main(String[] args) {
 5         // TODO Auto-generated method stub
 6         double a=30000;
 7         double sum=0;
 8         for(int i=1;i<=10;i++){
 9             a=a*(1+0.06);
10             sum+=a;
11         }
12         System.out.println("10年后的年薪为"+a);
13         System.out.println("总收入为"+sum);    
14     }
15 }
原文地址:https://www.cnblogs.com/zxp-0101/p/12604972.html