简体   繁体   中英

Do while loop repeats twice, doesn't follow conditions

I am new to Java and I've run into a bit of a problem. My instructor requires that I nest a do while loop within a while loop; the do while loop creates restrictions for the type of grade the user can input (more than 0 but less than 100) for the midterm, final and project grades. This input is then used in a calculation for the average of those three grades.

However, my do while loop runs twice and uses the input from the second prompt in the calculation for average. What am I doing wrong? I've gone through my textbook and this site, but I can't seem to understand why it's doing this since it's only prompting twice and then only using the input for the second prompt. Any help would be greatly appreciated. Thank you for taking the time to read my post!

For clarification, here is my code:

System.out.println("******** Step 2: Grading for Multiple Students *********");
    System.out.println();
    // Create scanner object for text and number
    Scanner textReader = new Scanner(System.in);
    Scanner numberReader = new Scanner(System.in);
    System.out.print("Would you like to input grades (y/n): ");
    String response = textReader.nextLine();
    // Create while loop for first yes/no question
    while ((!response.equalsIgnoreCase("y")) && (!response.equalsIgnoreCase("n")))
    {
        System.out.print("y/n needed: ");
        response = textReader.nextLine();
        System.out.println();
    }
    while ((response.equalsIgnoreCase("y")))
    {
        System.out.print("\nStudent Name: ");
        String studentName = textReader.nextLine();
        System.out.print("Course Name: ");
        String courseName = textReader.nextLine();
        System.out.print("Midterm Grade (needs to be between 0-100): ");
        double midtermGrade = numberReader.nextDouble();
        do
        {
            System.out.print("Midterm Grade (needs to be between 0-100): ");
            midtermGrade = numberReader.nextDouble();
            break;
        }
        while ((midtermGrade < 0) || (midtermGrade > 100));
        System.out.print("Final Grade (needs to be between 0-100): ");
        double finalGrade = numberReader.nextDouble();
        do
        {
            System.out.print("Final Grade (needs to be between 0-100): ");
            finalGrade = numberReader.nextDouble();
        }
        while ((finalGrade < 0) || (finalGrade > 100));
        System.out.print("Project Grade (needs to be between 0-100): ");
        double projectGrade = numberReader.nextDouble();
        do
        {
            System.out.print("Project Grade (needs to be between 0-100): ");
            projectGrade = numberReader.nextDouble();
        }
        while ((projectGrade < 0) || (projectGrade > 100));

And here is my output:

Output link

In a do-while loop, the loop is called at least once, regardless of the conditions. In a normal while loop, the loop is only called if the conditions are met.

Instead of

System.out.print("Final Grade (needs to be between 0-100): "); // remove this
double finalGrade = numberReader.nextDouble(); // remove this
do
{
   System.out.print("Final Grade (needs to be between 0-100): ");
   finalGrade = numberReader.nextDouble();
}
while ((finalGrade < 0) || (finalGrade > 100));

Simply remove the first two lines of the above snippet for each input loop.

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