0

我试图找到从控制台输入的两个最大的数字。我找到了第一个,但第二个的解决方案不起作用。程序正在编译并运行。这是代码。

import java.util.Scanner;

public class FindingSecondHighestScore_4_09 {

    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);

        double max = 1;
        double score2 = 0;
        String firstName = "";
        String secondName = null;
        System.out.println("Enter number of students: ");
        int x = input.nextInt();
        while(x > 0)
        {
            System.out.println("Enter Sudent's name");
            String name = input.next();
            System.out.println("Enter Student's score");
            double score = input.nextDouble();

            //find max
            if(score > max)
            {
                max = score;
                firstName = name;
            }

            //find second max
            if(max < score2  || score < score2)
            {
                max = score2;
                score = score2;
            }
            else if(max > score2  && score2 < score)
            {
                score2 = score;
                secondName = name;
            }


            x--;
        }
        System.out.println("The student: " + firstName + " has the greatest score: " + max);
        System.out.println("Second studemt " + secondName + " with second results: " + score2);

    }

}
4

4 回答 4

1

由于这看起来像家庭作业,因此我将给您一些提示:

  • 当你找到一个新的max,应该score2怎么办?
  • score2即使你找到了一个新的,你是否应该寻找一个新的max
于 2012-07-12T06:48:44.930 回答
1

如果我们想解决 if 结构,请考虑重新排列为以下内容:

if (/* new score beats second score, but not first */) {
    // replace second score
} else if (/* new score beats both first and second */) {
    // move first score down to second
    // assign a new first score
}

让您的思维过程与代码紧密对应,这将阐明每个块应该做什么,从而定位任何逻辑错误。

于 2012-07-12T06:50:22.290 回答
1

我认为当分数大于最大值时,必须将最大值转移到第二个分数并用新分数设置最大值....当分数在 max 和 score2 之间时,只能用新分数更新 score2

        //find max
        if(score > max)
        {
            score2 = max;
            max = score;
            secondName = firstName;
            firstName = name;
        }

        //find second max
        if(score < max && score > score2)
        {
            score2 = score;
            secondName = name;
        }
于 2012-07-12T07:29:39.110 回答
1

这是一个更详细的实现(我今天的起床练习):

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class TopScores {

    private static final int TOP_SELECTION_SIZE = 2;

    public static class Student {
        private final String name;
        private double score;

        public Student(String name) {
            if (name == null || name.length() == 0) {
                throw new IllegalArgumentException("Name cannot be empty");
            }
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public double getScore() {
            return score;
        }

        public void setScore(String score) {
            try {
                this.score = Double.parseDouble(score);
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException("Illegal score: " + score);
            }
        }

        @Override
        public String toString() {
            return String.format("%s with score %s", name, score);
        }
    }

    public static void main(String[] args) {

        List<Student> students = new ArrayList<TopScores.Student>();
        System.out.println("Please enter students. Press <RETURN> to stop.");
        Scanner input = new Scanner(System.in);
        boolean enteringData = true;
        while (enteringData) {
            try {
                System.out.print("Enter student's name: ");
                Student student = new Student(input.nextLine());
                System.out.print("Enter student's score: ");
                student.setScore(input.nextLine());
                for (int i = 0; i < students.size(); i++) {
                    if (student.getScore() > students.get(i).getScore()) {
                        students.add(i, student);
                        break;
                    }
                }
                if (students.size() == 0) {
                    students.add(student);
                }
            } catch (IllegalArgumentException e) {
                enteringData = false;
            }
        }

        int studentsToDisplay = Math.min(TOP_SELECTION_SIZE, students.size());
        if (studentsToDisplay > 0) {
            System.out.println("Top students:");
            for (int i = 0; i < studentsToDisplay; i++) {
                System.out.println("* " + students.get(i));
            }
        } else {
            System.out.println("No students to display");
        }
    }
}

我创建了一个单独的学生类,它保存姓名和分数,验证输入并为一个学生创建显示格式。

为了确定最高分,我通过将每个新学生添加到正确的位置来将所有输入的学生排序在一个列表中。

用户不必事先输入学生人数,但可以通过输入空行(或无效分数)来终止数据输入。

数据输入完成后,将打印所需数量的得分最高的学生。

这种方法更灵活;打印前 3 名或前 10 名学生只需更改 TOP_SELECTION_SIZE 的值即可。

最重要的收获:尽可能在课堂上思考(在这种情况下是学生),并将合理的责任委派给每个课堂。

于 2012-07-12T07:46:26.640 回答