简体   繁体   中英

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. 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)?

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 . Then you wouldn't have parallel variables - you'd just have topStudent , secondStudent , currentStudent to worry about.

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:

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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