我建议您使用 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
希望这可以帮助..
谢谢..快乐编码...