我的任务是完成一个用户输入学生姓名和分数的程序。最后,程序应该输出得分最高的两个学生,但我只能输出得分最高的学生。
public class StudentScores
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of students: ");
int numStudents = input.nextInt();
System.out.print("Enter the student's name: ");
String Student1 = input.next();
System.out.print("Enter the student's score: ");
int Score1 = input.nextInt();
for (int i = 0; i < numStudents - 1; i++)
{
System.out.print("Enter the student's name: ");
String Student = input.next();
System.out.print("Enter the student's score: ");
int Score = input.nextInt();
if (Score > Score1)
{
Student1 = Student;
Score1 = Score;
}
}
System.out.println(Student1 + "'s score is " + Score1);
}}
我不确定的是如何根据用户输入计算出如何在混合中获得 Student2 和 Score 2。我想使用数组,但我必须使用循环,所以这就是我难住的地方。