Help us understand the problem. What is going on with this article?

配列の練習2

More than 1 year has passed since last update.

ArrayPracticeクラスのフィールド部分で
int[][]arr= new int[3][students];と入れたかったが
エラーが出たので諦め。できるだけ読みやすくなるよう努めた。
メソッドの内容は前の配列練習の文を元に少し手を加えた。

ArrayExam.java
package JavaStudy;
//メソッドを分けて配列の練習
//テスト結果(教科名・平均点・科目別最高点・生徒数)を出力

public class ArrayExam {

    public static void main(String[] args) {
        final int math = 0;
        final int english = 1;
        final int science = 2;

        ArrayPractice t = new ArrayPractice(20);
        t.startExam(math);
        t.startExam(english);
        t.startExam(science);

        t.printResult(math);
        t.printResult(english);
        t.printResult(science);
    }
}
ArrayPractice.java
package JavaStudy;
import java.util.Random;
//各項目出力用のメソッドをまとめた
public class ArrayPractice {
    final String[] SUBJECT= {"算数","英語","理科"};
    int students = 0;
    int [][] arr;
    //インスタンス生成時にクラスの生徒数を受け取る
    ArrayPractice(int students){
        this.students = students;
        arr = new int[3][students];
    }

    void printResult(int sub) {
        //結果を出力する
        System.out.println("試験科目は "+ SUBJECT[sub]+"です。");
        System.out.println("生徒数は "+ students +"人です。");
        System.out.println("平均点は "+ resultAverage(sub)+"点です。");
        System.out.println("最高点は "+ getMax(sub)+"点です。");        
        System.out.println("-----------");
    }
    int getMax(int sub) {
        //科目別の最高点を返す
        int max = 0;
        for(int i = 0;i < students;i++) {
            if(arr[sub][i] > max) 
                max = arr[sub][i];
        }
        return max;
    }
    void startExam(int sub) {
        //ランダムな点数(0〜100点)を教科の配列にいれる
        Random random = new Random();
        for(int i = 0;i < students ;i++) 
            arr[sub][i] = random.nextInt(101);      
    }
    int resultAverage(int sub) {
        //平均点を返す
        int sum = 0;
        int average = 0;
        for(int i =0;i < students;i++) 
            sum = sum + arr[sub][i];
        average = sum/students;
        return average;
    }
}
sumy
Java勉強中です。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away