java Java最高分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32808033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java highest score
提问by Jake
Okay so i'm trying to make a program to display the top 2 highest scorers of 5 students only.
好的,所以我正在尝试制作一个程序,只显示 5 名学生的前 2 名最高分。
So sample output.
所以样本输出。
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!
I only know how to take the largest score and not the second here is what i got so far.
我只知道如何获得最大的分数,而不是第二个,这是我目前得到的。
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?
回答by Manos Nikolaidis
You may want to look at sorting methods to solve such problems in the future e.g. sorting Arraysand sorting collections
您可能希望查看排序方法来解决将来的此类问题,例如排序数组和排序集合
For your particular case where you want to select the two max elements you can simply use two variables
对于您想要选择两个最大元素的特殊情况,您可以简单地使用两个变量
int highestScore = 0;
String highestName = "";
int secondScore = 0;
String secondName = "";
and then
接着
if (num > highestScore) {
secondScore = highestScore;
secondName = highestName;
highestScore = num;
highestName = name;
} else if (num > secondScore) {
secondScore = num;
secondName = name;
}
The code may be cleaner if you define a Student class to hold score and name.
如果您定义一个 Student 类来保存分数和名称,代码可能会更清晰。
Printing is straightforward
打印很简单
System.out.printnl("Congratulations " + highestName + "!");
System.out.printnl("Congratulations " + secondName + "!");
回答by jste89
To expand on what Manos said:
扩展 Manos 所说的内容:
You probably want to create a class for your students:
您可能想为您的学生创建一个类:
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;
}
}
You can then add each student to a collection and use a Comparator to sort your students:
然后,您可以将每个学生添加到一个集合中,并使用比较器对您的学生进行排序:
Collections.sort(students, new Comparator<Student>() {
public int compare(Student o1, Student o2) {
return Integer.compare(o1.getScore(), o2.getScore());
}
});
The resulting collection will hold a list where the highest scoreing students will be at the far end of the collection, or you can then reverse the collection so they are at the begining instead:
生成的集合将包含一个列表,其中得分最高的学生将位于集合的远端,或者您可以反转集合,使他们位于开头:
Collections.reverse(students);
Full example:
完整示例:
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()) );
}