0

我对我的代码有一个简单的问题。我对java很陌生,并试图自学,但我现在有点陷入循环。对我来说,这似乎应该有效。问题是要求一些学生,然后让用户输入每个学生的姓名和分数。然后它应该显示得分最高的学生和第二高的学生。出于某种原因,我的代码只显示了我为第一高分和第二高分输入的名字和分数。我可能犯了一些大错误,但也许有人可以指出我正确的方向?对不起,如果这看起来像一个巨大的混乱。:(

public class Chapter4_9 {

  public static void main(String[] args) {

    //scanner for input
    Scanner input = new Scanner(System.in);

    //ask user for number of students
    System.out.print("Enter the number of students: ");
    int numberStudents = input.nextInt();

    //declare variables
    double highestScore = 0;
    double tempScore = 0;
    double secondHighestScore = 0;
    String firstStudent = "";
    String tempStudent = "";
    String secondStudent = "";

    for (int i = 0; numberStudents != i; ++i) {
        System.out.print("Enter the students name followed by his score: ");
        String studentName = input.next();
        double studentScore = input.nextDouble();

        if (i == 0){
            firstStudent = studentName;
            highestScore = studentScore;
        }

        else if (studentScore > highestScore) {
            tempStudent = firstStudent;
            studentName = firstStudent;
            secondStudent = tempStudent;
            tempScore = highestScore;
            studentScore = highestScore;
            secondHighestScore = tempScore;
        }


    }   
    System.out.println("The highest scoring student is " + firstStudent + " with a " + highestScore);
    System.out.println("The second highest scoring student is " + secondStudent + " with a " + secondHighestScore);

}
}
4

4 回答 4

1

这个块似乎有点混乱:

else if (studentScore > highestScore) {
    tempStudent = firstStudent;
    studentName = firstStudent;
    secondStudent = tempStudent;
    tempScore = highestScore;
    studentScore = highestScore;
    secondHighestScore = tempScore;
}

这个块的预期结果是什么?为什么你要覆盖 and 的值studentNamestudentScore当它们不再被读取时(在你从用户那里读取新值之前)?

大概目的是用最高分数/名称替换第二个分数/名称,然后用当前输入替换最高分数-但这根本不是代码的作用。这会做到:

secondStudent = firstStudent;
secondScore = highestScore;
firstStudent = studentName;
highestScore = studentScore;

根本不需要临时变量。

然而,仅仅这样的改变是不够的。您需要考虑新分数不高于当前最高分但高于当前第二高分的情况。我会让你弄清楚这需要什么......

顺便说一句,如果您为“名称/分数”组合引入了一个单独的类,您的代码可能会更简单,例如Student. 那么你就没有并行变量了——你只需要担心topStudent, secondStudent, 。currentStudent

于 2012-08-04T08:24:36.980 回答
0

当您发现更高的分数时,您的代码是错误的。

secondStudent = fistStudent; // what used to be high score is now 2nd
firstStudent = studentName;
// score adjustment left for you to do ;)
于 2012-08-04T08:24:21.430 回答
0

代码中有一个错误,而且您并没有涵盖所有内容。

在 for 循环中,您必须拥有这些:

else if (studentScore > highestScore) {
        secondStudent = firstStudent;
        firstStudent = studentName;
        secondHighestScore = highestScore;
        highestScore = studentScore;
    }
else if (studentScore < highestScore && studentScore > secondHighestScore) {
        secondStudent = studentName;
        secondHighestScore = studentScore;
    }

完毕。

于 2012-08-04T08:27:18.553 回答
0

你的逻辑不正确。你没有处理所有方面。如果您检查您的代码,它只会正确处理第一个输入,这完全取决于您如何提供输入。你的逻辑需要改进。没有必要有这么多的临时变量。

您最好在调试模式下运行您的应用程序并进入它,这样您就知道哪里出了问题。

于 2012-08-04T08:36:13.460 回答