简体   繁体   English

Java循环的一个简单程序问题

[英]A simple program issue with java loops

I have a simple question about my code. 我对我的代码有一个简单的问题。 I'm quite new to java and trying to self learn but im kind of stuck on loops right now. 我对Java很陌生,尝试自我学习,但是我现在陷入了循环。 To me it seems like this should work. 在我看来,这应该可行。 The problem is too ask for a number of students, then have the user input the names and scores of each student. 问题也是要问一些学生,然后让用户输入每个学生的姓名和分数。 Then it should display the first and second highest scoring students. 然后,它应该显示得分最高的学生和第二名的学生。 For some reason my code just shows the first name and score I enter for both the first highest score and the second highest score. 出于某种原因,我的代码仅显示了我输入的第一名和第二名的名字和分数。 I probably made some big mistake but maybe somebody can point me in the right direction? 我可能犯了一个大错误,但也许有人可以指出我正确的方向吗? Sorry if this looks like a huge mess. 抱歉,这看起来像是一团糟。 :( :(

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

}
}

This block seems a little muddled: 这个块似乎有点混乱:

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

What is the intended consequence of this block? 此阻止的预期结果是什么? Why are you overwriting the value of studentName and studentScore , when they're never read again (before you read new values from the user anyway)? 当您再也无法再次读取studentNamestudentScore的值时(在从用户那里读取新值之前),为什么要覆盖它们的值?

Presumably the aim is to replace the second score/name with the highest score/name, and then replace the highest ones with the current input - but that's not what the code does at all. 大概的目的是用最高的分数/名称替换第二个分数/名称,然后用当前输入替换最高的分数/名称-但这根本不是代码所要做的。 This would do it: 这样做:

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

No need for temporary variables at all. 完全不需要临时变量。

However, just that change isn't enough. 但是,仅此更改是不够的。 You also need to consider the situation where the new score isn't higher than the current highest score, but is higher than the current second highest score. 需要考虑新分数不高于当前最高分数,但高于当前第二最高分数的情况。 I'll leave you to work out what that requires... 我让你去解决需要什么...

By the way, your code would probably be simpler if you introduced a separate class for the "name/score" combination, eg Student . 顺便说一句,如果为“名称/分数”组合引入了单独的类,例如Student ,则代码可能会更简单。 Then you wouldn't have parallel variables - you'd just have topStudent , secondStudent , currentStudent to worry about. 然后,您将没有并行变量-只需担心topStudentsecondStudentcurrentStudent即可。

Your code when you find a higher score is wrong. 当您发现分数更高时,您的代码是错误的。

secondStudent = fistStudent; // what used to be high score is now 2nd
firstStudent = studentName;
// score adjustment left for you to do ;)

There is one mistake in the code and also you are not covering everything. 代码中有一个错误,并且您没有涵盖所有内容。

In the for loop, you have to have these: 在for循环中,您必须具有以下这些:

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

Done. 做完了

Your logic is not correct. 您的逻辑不正确。 You are not handling all the aspects . 您没有处理所有方面。 If you check your code , it will just handle first inputs correctly and it all depends on how you give your inputs. 如果检查您的代码,它将仅能正确处理第一个输入,而这完全取决于您提供输入的方式。 Your logic needs to be improved. 您的逻辑有待改进。 There is no need to have so many temporary variables. 不需要那么多的临时变量。

It would be good you run your application in debug mode and step into it so that you know where it goes wrong . 最好在调试模式下运行您的应用程序,然后逐步进入它,以便知道它出了什么问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM