我負責完成用戶輸入學生姓名及其分數的程序。最後,該計劃應輸出分數最高的兩名學生,但我只能輸出分數最高的學生。根據輸入查找兩個最高數字
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和Score2。我想使用數組,但我必須使用循環,所以這是我難倒的地方。
此外,我們建議您使用的「駝峯」的命名變量的Java約定,如'score1'和'student1',以避免與類名混淆。 – iamnotmaynard 2013-02-25 23:12:54
http://docs.oracle.com/javase/tutorial/collections/interfaces/order.html – 2013-02-25 23:16:02
好主意@iamnotmaynard - 並感謝您的鏈接。我相信我已經想出了我的問題。我之前使用了Else If語句,並且得到了相反的結果,但現在我看到了我的問題。謝謝。我也想補充我不能使用數組。感謝您的想法,但如上所述...... – Hydlide 2013-02-25 23:21:53