第四次课下作业

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

package qq;

import java.util.Scanner;

public class Rr {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
        int he=0;
        for(int x=1;x<=100;x++){
            if(x%3==0){
                he+=x;
            }
        }System.out.println("和是"+he);
    }

}
package qq;

import java.util.Scanner;

public class Rr {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
        int he=0;
        int x=1;
        while(x<=100){
            if(x%3==0){
                he+=x;
                
            }
            x++;
        }
        System.out.println("和是"+he);
    }

}
package qq;

import java.util.Scanner;

public class Rr {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
        int he=0;
        int x=1;
        do{
            if(x%3==0){
                he+=x;
                
            }
            x++;
        }while(x<=100);
        System.out.println("和是"+he);
    }

}

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

package qq;

import java.util.Scanner;

public class Rr {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
       for(int x=1;x<9;x++){
           if(x==5){
             continue;
           }
        System.out.print(x);}
    }

}

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

package qq;

import java.util.Scanner;

public class Rr {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
        System.out.println("请输入x的值");
        int x=input.nextInt();
        int jc=1;
       for(int j=1;j<=x;j++){
           jc*=j;
           }
        System.out.print(x+"的阶乘是"+jc);
        }
}

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

package qq;
import java.util.Scanner;

public class Cc {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
           int i = 0;
            for (i = 0 ; ; i++) {
                System.out.print("请输入成绩:");
                int score = input.nextInt();
                if (score < 0 || score > 100) {
                    System.out.println("输入成绩错误,请重新输入!");
                } else {
                    System.out.println("该学生成绩为:" + score);
                }
            }
    }

}

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

package qq;
import java.util.Scanner;

public class Cc {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
        double nx=30000;
        double he=30000;
        for(int i=2;i<=10;i++){
            nx=nx*(1+0.06);
            he+=nx;
        }
        
        System.out.println("十年后的工资为"+nx+"\n十年内的总工资"+he);
    }

}

 

posted @ 2020-03-30 16:06  maksvil  阅读(142)  评论(0编辑  收藏  举报