2013-02-25 60 views
-1

我負責完成用戶輸入學生姓名及其分數的程序。最後,該計劃應輸出分數最高的兩名學生,但我只能輸出分數最高的學生。根據輸入查找兩個最高數字

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。我想使用數組,但我必須使用循環,所以這是我難倒的地方。

+4

此外,我們建議您使用的「駝峯」的命名變量的Java約定,如'score1'和'student1',以避免與類名混淆。 – iamnotmaynard 2013-02-25 23:12:54

+0

http://docs.oracle.com/javase/tutorial/collections/interfaces/order.html – 2013-02-25 23:16:02

+0

好主意@iamnotmaynard - 並感謝您的鏈接。我相信我已經想出了我的問題。我之前使用了Else If語句,並且得到了相反的結果,但現在我看到了我的問題。謝謝。我也想補充我不能使用數組。感謝您的想法,但如上所述...... – Hydlide 2013-02-25 23:21:53

回答

3
if (Score > Score1) 
{ 
    Student2 = Student1; 
    Score2 = Score1; 
    Student1 = Student; 
    Score1 = Score; 
} 
else if (Score > Score2) 
{ 
    Student2 = Student; 
    Score2 = Score; 
} 
+0

好吧,這是我原來的,但現在我想我明白如何使用它。我得到了相反的結果,現在我明白了爲什麼。謝謝。 – Hydlide 2013-02-25 23:19:48

1

您可以將學生存儲在數組中,然後對數組進行排序並返回前兩個。

或者如果你只想存儲最高的兩個分數,那麼你需要一些嵌套的比較器。

if (score > score2) { 
    if (score > score1) { 
     student2 = student1; 
     score2 = score1; 
     student1 = student; 
     score1 = score; 
    } else { 
     student2 = student; 
     score2 = score; 
    } 
} 
+0

再一次地,排在第一位的應該撞到第二位。給定[1,2,3,4]的分數,你的代碼將返回4分數1,但1分數2。 – 2013-02-25 23:35:29

+0

@MarkReed很好的捕捉。 – 2013-02-25 23:40:29

1

您將需要另一對變量Student2Score2爲了跟蹤第二個學生。您可以將這兩個分數初始化爲一個低於最小值的值(例如,如果分數範圍爲0到10,則可以將它們初始化爲-1);

String Student1 = "none", Student2 = "none"; 
int Score1 = -1, Score2 = -1; 


for (int i = 0; i < numStudents; 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) 
    { 
     Student2 = Student1; 
     Score2 = Score1; 
     Student1 = Student; 
     Score1 = Score; 
    } 
    else if (Score > Score2) {    
     Student2 = Student; 
     Score2 = Score; 
    } 
} 
+0

Acutally它應該是(我 Javier 2013-02-25 23:15:16

+1

該邏輯是不正確的。如果Score1應該是最高分,那麼將學生從其中撞到它應該將它們撞到* Score2(如我的答案中)。另一方面,如果Score2是最高分,那麼這會對他們進行錯誤排序。 – 2013-02-25 23:15:17

+0

對不起,我的錯。 – Javier 2013-02-25 23:20:12

1

您可以在Java中使用Arrays.sort() method。做到這一點的正確方法是創建一個名稱和分數的「學生」對象。這將是這個樣子:

public class Student implements Comparable { 
    private int score; 
    private String name; 

    public Student(String name, int score) 
    { // Constructor Code } 

使對象實現Comparable接口,這意味着寫compare method。在對學生數組進行排序後,打印數組中的前兩個名字。

1

爲了方便排序我建議類Student處理雙方比分並實現Comparable<Student>名稱(這會爲學生的任何作業量)。然後你必須編寫compareTo(Student student)方法,如果得分this大於student,則返回1,如果它們相等則返回0,否則返回-1。然後,您可以使用Collections.sort()(對於Collections,即ArrayList)和Arrays.sort()

1

一個TreeMap<Integer, String>可以在這裏很有用,它是根據其鍵的自然順序進行排序:

TreeMap<Integer, String> map = new TreeMap<Integer, String>(); 

Scanner input = new Scanner(System.in); 
System.out.print("Enter the number of students: "); 
int numStudents = input.nextInt(); 

for (int i = 0; i < numStudents; 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(); 
    map.put(Score, Student); 
} 

Map.Entry<Integer, String> entry1 = map.pollLastEntry(); 
Map.Entry<Integer, String> entry2 = map.pollLastEntry(); 
System.out.println("Highest score: " + entry1.getKey()); 
System.out.println("Highest scorer: " + entry1.getValue()); 
System.out.println("Second highest score: " + entry2.getKey()); 
System.out.println("Second highest scorer: " + entry2.getValue()); 

尋找第三,第四等最高分/得分將是這種方法容易得多。

0
package deneme; 

import java.util.Scanner; 

public class HighestTwoScore { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 

     Scanner input = new Scanner(System.in); 

     System.out.print("Enter the number of student: "); 
     int numberOfStudents = input.nextInt(); 

     System.out.print("Enter the Students name and score: "); 
     String name1 = input.next(); 
     int score1 = input.nextInt(); 

     System.out.print("Enter the Students name and score: "); 
     String name2 = input.next(); 
     int score2 = input.nextInt(); 

     int controlScore = score1; 
     String controlName = name1; 
     if (score1 < score2) { 
      score1 = score2; 
      score2 = controlScore; 
      name1 = name2; 
      name2 = controlName; 
     } 

     int i = 0; 
     while (i < numberOfStudents - 2) { 
      System.out.print("Enter the Students name and score: "); 
      String name = input.next(); 
      int score = input.nextInt(); 

      if (score > score1) { 
       score2 = score1; 
       name2 = name1; 
       score1 = score; 
       name1 = name; 

      } else if (score > score2) { 
       score2 = score; 
       name2 = name; 
      } 

      i++; 
     } 
     System.out.println("Top student1 " + name1 + "'s score is " + score1); 
     System.out.println("Top student2 " + name2 + "'s score is " + score2); 
    } 

} 
+0

(一些解釋會很好...)[http://stackoverflow.com/help/how-to-answer] – 2014-05-23 20:37:14

0
import java.util.Scanner; 

public class TwoLargestNumbers{ 

    public static void main(String[] args) { 
     System.out.println("Enter number: "); 

     Scanner input = new Scanner(System.in); 

     int firstLarge = 0, secondLarge = 0; 

     for (int i = 1; i <= 10; i++) { 
      int num = input.nextInt(); 

      if (num >= firstLarge) { 
       secondLarge = firstLarge; 
       firstLarge = num; 
      } 

      if (num > secondLarge && num < firstLarge) 
       secondLarge = num; 

     } 

     System.out.println("First large number is: " + firstLarge); 
     System.out.print("Second large number is: " + secondLarge); 
    } 
} 
+0

你正在檢查的數字大於secondLarge,然後如果它是真的,你將firstlarge分配爲num。我認爲你的邏輯是錯誤的。例如:secondLarge爲20,firstLarge爲30,num爲25.你將使secondlarge爲30,firstlarge爲25.然後在下一次迭代中,如果num爲27,則忽略它,因爲secondlarge是30.所以第一個和第二大將仍然是30和25而不是30和27 – phoenix 2016-02-05 19:37:09

+0

對不起,讚揚修改。 – gadolf 2016-02-05 20:07:05

相關問題