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

4

8 回答 8

3
if (Score > Score1) 
{
    Student2 = Student1;
    Score2 = Score1;
    Student1 = Student;
    Score1 = Score;
}
else if (Score > Score2)
{
    Student2 = Student;
    Score2 = Score;
}
于 2013-02-25T23:13:09.327 回答
1

您可以将学生存储在一个数组中,然后对数组进行排序并返回前两个。

或者如果您只想存储最高的两个分数,那么您将需要一些嵌套比较器。

if (score > score2) {
    if (score > score1) {
        student2 = student1;
        score2 = score1;
        student1 = student;
        score1 = score;
    } else {
        student2 = student;
        score2 = score;
    }
}
于 2013-02-25T23:08:35.543 回答
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;
    }
}
于 2013-02-25T23:11:33.883 回答
1

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

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

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

让对象实现Comparable接口,也就是写一个compare 方法。对学生数组进行排序后,打印数组中的前两个姓名。

于 2013-02-25T23:13:00.590 回答
1

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

于 2013-02-25T23:19:33.740 回答
1

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

使用这种方法,找到第三、第四等最高分/得分者会容易得多。

于 2013-02-25T23:19:52.380 回答
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);
    }

}
于 2014-05-23T20:10:48.567 回答
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);
    }
}
于 2016-02-05T19:14:13.743 回答