4回目の宿題

10115 ワード

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.1つのプログラムを編纂して、整数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+"
"+he); } }