Java得分最高

时间:2015-09-27 12:43:52

标签: java for-loop

好的,所以我试图制作一个节目,只显示5名学生的前2名最高分。

示例输出。

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?

2 个答案:

答案 0 :(得分: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;
}

如果您定义Student类来保存分数和名称,则代码可能更清晰。

打印很简单

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

答案 1 :(得分: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());
    }
});

最终的收藏将保留一个列表,其中得分最高的学生将位于收藏的远端,或者您可以反转收集,以便他们在开始时:

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()) );
}