我建议您使用Map集合和枚举常量。
此外,测验类型和分数是紧密相关的数据,因此它们应该在单独的类别中。
为简单起见,请检查以下代码:
枚举类,用于存储测验的类型。
public enum QuizType { SCIENCE, MATHS;}QuizResult是一个POJO类,仅用于保存有关测验结果的数据。
public class QuizResult { private QuizType quizType; private double score; public QuizResult(QuizType quizType, double score) { this.quizType = quizType; this.score = score; } public QuizType getQuizType() { return quizType; } public void setQuizType(QuizType quizType) { this.quizType = quizType; } public double getScore() { return score; } public void setScore(double score) { this.score = score; }}StudentQuiz类用于输入操作。评论应解释一切。
public class StudentQuiz { public StudentQuiz() { Map<String, List<QuizResult>> students = processStudentQuizes(); // Go through the keys of the map (which is the name of students) for(String student : students.keySet()) { // Get all the quiz results of the student List<QuizResult> results = students.get(student); System.out.println(student + " "); for(QuizResult result : results) { // Print the quiz type and the score System.out.println(result.getQuizType().name() + " " + result.getScore()); } } } private Map<String, List<QuizResult>> processStudentQuizes() { Map<String, List<QuizResult>> students = new TreeMap<>(); System.out.println("Add student"); Scanner s = new Scanner(System.in); String answer = null; while(!"n".equalsIgnoreCase(answer)) { System.out.println("Student Name"); String studentName = s.next(); System.out.println("Quiz Name"); String quizName = s.next(); // Check if the quiz type actually exist boolean quizTypeOk = false; for(QuizType type : QuizType.values()) { if(type.name().equalsIgnoreCase(quizName)) { // We found the quiz type, so we can break from this loop quizTypeOk = true; break; } } if(!quizTypeOk) { // Quiz type couldn't be found, so print the message and start the while loop again System.out.println("No such type of quiz! Please start again!"); continue; } System.out.println("Average Score"); double avgScore = s.nextDouble(); // Now we can create a QuizResult object from the inputed data QuizResult result = new QuizResult(QuizType.valueOf(quizName), avgScore); // Check if the student already exist if(students.containsKey(studentName)) { // Add a new result to the list of results for the already existing student students.get(studentName).add(result); } else { // Create a new student in the map with a new results list and put the result into the list List<QuizResult> results = new ArrayList<>(); results.add(result); students.put(studentName, results); } System.out.println("Add another Student Y/N"); answer = s.next(); } s.close(); return students; } public static void main(String[] args) { new StudentQuiz(); }}我希望这有帮助。



