根据输入找到两个最高的数字

时间:2013-02-25 23:06:22

标签: java

我的任务是完成一个用户输入学生姓名和分数的程序。最后,该计划应输出得分最高的两名学生,但我只能输出得分最高的学生。

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。我想使用数组,但我必须使用循环,所以这是我难倒的地方。

7 个答案:

答案 0 :(得分:3)

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

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

答案 2 :(得分:1)

您可以在Java中使用Arrays.sort() method。正确的方法是创建一个&#34;学生&#34;具有名称和分数的对象。它看起来像这样:

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

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

使对象实现Comparable接口,这意味着编写compare method。当您对学生数组进行排序后,打印数组中的前两个名称。

答案 3 :(得分:1)

为了便于排序(适用于任何数量的学生),我建议班级Student处理得分和实施Comparable<Student>的名称。然后你必须编写compareTo(Student student)方法,如果this的分数大于student,则应该返回1,如果它们相等则为0,否则为-1。然后,您可以使用Collections.sort()(适用于Collections,即ArrayList)和Arrays.sort()

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

使用这种方法可以更轻松地找到第三,第四等最高得分/得分者。

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

}

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