2015-09-27 63 views
-1

好吧,我正在嘗試製作一個程序,僅顯示5位學生排名前2的最高得分者。Java最高分

所以示例輸出。

Enter your name : Spear 
Enter score : 56 

Enter your name : Sky 
Enter score : 61 

Enter your name : Spy 
Enter score : 45 

Enter your name : Raks 
Enter score : 31 

Enter your name : Felicio 
Enter score : 39 


Congratulations Sky! 
Congratulations Spear! 

我只知道如何取最大的分數,而不是第二個這裏是我到目前爲止。

import java.util.Scanner; 

public class Highest{ 
    public static void main(String[]args) { 

     Scanner x = new Scanner(System.in); 
     String name = ""; 
     int score; 
     int k; 
     int highest = 0; 

     num = x.nextLine(); 
     largest = num; 

     for (int i = 0; i <= 5; i++) { 
      System.out.print("Enter name: "); 
      k = x.nextInt(); 
      System.out.print("Enter score: "); 
      num = x.nextInt(); 
      if (num > highest) { 
       highest = num; 
      } 
     } 
     System.out.println(largest); 
    } 
} // how do i display the name of the highest score and the second placer? 

回答

3

你可能想看看排序方法來解決未來的這類問題,例如, sorting Arrayssorting collections

您的特定情況下,要選擇兩個最大的元素,你可以簡單地使用兩個變量

int highestScore = 0; 
String highestName = ""; 

int secondScore = 0; 
String secondName = ""; 

然後

if (num > highestScore) { 
    secondScore = highestScore; 
    secondName = highestName; 

    highestScore = num; 
    highestName = name; 
} else if (num > secondScore) { 
    secondScore = num; 
    secondName = name; 
} 

如果你定義的代碼可能是更清潔一個學生課程來保持分數和名字。

印刷是直接

System.out.printnl("Congratulations " + highestName + "!"); 
System.out.printnl("Congratulations " + secondName + "!"); 
+0

我如何打印他們的名字? – Jake

+0

我想我有一個邏輯錯誤在我的循環如何保持他們的名字。 – Jake

+0

我應該爲名稱創建一個數組嗎?因爲當我輸入名字,然後我去鍵入第二我不能。 – Jake

1

要展開什麼馬諾斯說:

你可能想創建一個類爲您的學生:

class Student { 
    private String name; 
    private int score; 

    Student(String name, int score) { 
     this.name = name; 
     this.score = score; 
    } 

    public int getScore() { 
     return this.score; 
    } 

    public String getName() { 
     return this.name; 
    } 
} 

您可以將每個學生再加入到收集並使用比較器對學生進行分類:

Collections.sort(students, new Comparator<Student>() { 
    public int compare(Student o1, Student o2) { 
     return Integer.compare(o1.getScore(), o2.getScore()); 
    } 
}); 

返回的集合將舉行一個列表,其中最高的scoreing學生將在收集的盡頭,或則你可以扭轉集合,以便他們在開始時,而不是:

Collections.reverse(students); 

完整的示例:

public static void main(String[] args) { 
    class Student { 
     private String name; 
     private int score; 

     Student(String name, int score) { 
      this.name = name; 
      this.score = score; 
     } 

     public int getScore() { 
      return this.score; 
     } 

     public String getName() { 
      return this.name; 
     } 
    } 

    ArrayList<Student> students = new ArrayList<>(); 

    for (int i = 0; i < 10; i++) { 
     java.util.Random rand = new java.util.Random(); 
     Student s = new Student("Student " + i, rand.nextInt()); 
     students.add(s); 
    } 

    Collections.sort(students, new Comparator<Student>() { 
     public int compare(Student o1, Student o2) { 
      return Integer.compare(o1.getScore(), o2.getScore()); 
     } 
    }); 
    Collections.reverse(students); 


    System.out.println("Highest scoring student: " + students.get(0).getName() + " with a score of " + students.get(0).getScore()); 
    System.out.println("Highest scoring student: " + students.get(1).getName() + " with a score of " + students.get(1).getScore()); 

    // List all students (Java 8 only...) 
    students.forEach(x -> System.out.println("Name: " + x.getName() + " with score: " + x.getScore())); 
}