Array practice 2

In the field part of the ArrayPractice class I wanted to put int [] [] arr = new int [3] [students]; I gave up because I got an error. I tried to make it as easy to read as possible. The content of the method has been slightly modified based on the previous sequence practice sentence.

ArrayExam.java


package JavaStudy;
//Divide the method and practice the array
//Output test results (subject name, average score, highest score by subject, number of students)

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;
//The methods for each item output are summarized.
public class ArrayPractice {
	final String[] SUBJECT= {"arithmetic","English","Science"};
	int students = 0;
	int [][] arr;
	//Receive the number of students in a class when instantiating
	ArrayPractice(int students){
		this.students = students;
		arr = new int[3][students];
	}

	void printResult(int sub) {
		//Output the result
		System.out.println("The exam subject is"+ SUBJECT[sub]+"is.");
		System.out.println("The number of students"+ students +"It is a person.");
		System.out.println("The average score is"+ resultAverage(sub)+"It is a point.");
		System.out.println("The highest point is"+ getMax(sub)+"It is a point.");		
		System.out.println("-----------");
	}
	int getMax(int sub) {
		//Return the highest score by subject
		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) {
		//Put a random score (0 to 100 points) in the subject array
		Random random = new Random();
		for(int i = 0;i < students ;i++) 
			arr[sub][i] = random.nextInt(101);		
	}
	int resultAverage(int sub) {
		//Returns the average score
		int sum = 0;
		int average = 0;
		for(int i =0;i < students;i++) 
			sum = sum + arr[sub][i];
		average = sum/students;
		return average;
	}
}


Recommended Posts

Array practice
Array practice 2
Array
Array
[Java] array
[Ruby] Array
Java array
[Practice] ArrayList
Java array
java (array)
[Practice] ArrayList
Java array
[Java] Array
Java array
ArrayList practice
12 of Array
[Practice] Enumeration
java array
[Java] Array
if else practice
java array variable
String Buffer practice
JavaScript array manipulation
[Java] Array notes
Array / list / map
[Practice] Map method
Array is object
Ruby two-dimensional array
Studying Java-Part 7-Array
Ruby array manipulation