学生オブジェクトを格納するには、arrayList を使用することをお勧めします。理解を深めるために、次の例を検討してください。
まず、getters() と setters() を使用して生徒の詳細を格納するモデル クラスを作成できます。次のようになります。
package com.stack.overflow.works.model;
public class Student {
private String firstName;
private String lastName;
private int score;
public Student() {}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
次に、以下に示すように StudentScoresApp を作成して、ユーザーからの入力を読み取ることができます。
package com.stack.overflow.works.main;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import com.stack.overflow.works.model.Student;
public class StudentScoresApp {
public static List<Student> getStudentScores() {
List<Student> students = new ArrayList<Student>();
Student student = null;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number of students to enter: ");
int numberOfStudents = scanner.nextInt();
for (int i = 0; i < numberOfStudents; i++) {
student = new Student();
System.out.print("Enter Student " + (i + 1) + " First Name:");
String firstName = scanner.next();
student.setFirstName(firstName);
System.out.print("Enter Student " + (i + 1) + " Last Name:");
String lastName = scanner.next();
student.setLastName(lastName);
System.out.print("Enter Student " + (i + 1) + " Score:");
int score = scanner.nextInt();
student.setScore(score);
students.add(student);
}
scanner.close();
return students;
}
public static void displayStudentScores(List<Student> students) {
int i = 1;
for (Student student: students) {
System.out.println("Student " + (i) + " First Name:" + student.getFirstName());
System.out.println("Student " + (i) + " Last Name:" + student.getLastName());
System.out.println("Student " + (i) + " Score:" + student.getScore());
i++;
}
}
public static void main(String[] args) {
System.out.println("Welcome to the Student Scores Application");
System.out.println("*****************************************");
List<Student> students = StudentScoresApp.getStudentScores();
System.out.println();
System.out.println("Displaying Student Scores:");
System.out.println("*************************");
StudentScoresApp.displayStudentScores(students);
}
}
これで、StudentScoresApp を実行できます。テスト結果の例を以下に示します。
Welcome to the Student Scores Application
*****************************************
Enter number of students to enter: 3
Enter Student 1 First Name:Sandeep
Enter Student 1 Last Name:Thulaseedharan
Enter Student 1 Score:100
Enter Student 2 First Name:Sathya
Enter Student 2 Last Name:Narayanan
Enter Student 2 Score:100
Enter Student 3 First Name:Jayakrishnan
Enter Student 3 Last Name:Lal
Enter Student 3 Score:100
Displaying Student Scores:
*************************
Student 1 First Name:Sandeep
Student 1 Last Name:Thulaseedharan
Student 1 Score:100
Student 2 First Name:Sathya
Student 2 Last Name:Narayanan
Student 2 Score:100
Student 3 First Name:Jayakrishnan
Student 3 Last Name:Lal
Student 3 Score:100
お役に立てれば..
ありがとう..幸せなコーディング...