简体   繁体   中英

do while loop is not breaking properly java

public class DoWhile1 {
    private static int grade, total, sum, average;

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

        do {
            System.out.println("Please enter a grade b/w 0-100:");
            grade = sc.nextInt();

            if (grade >= 0 && grade <= 100) {
                total++;
                sum = sum + grade;

                System.out.println("Please enter another grade or 999 to break:");
            } else {    
                System.out.println("Incorrect value, please reenter grade:");
            }
        } while (grade != 999);

        average = sum/total;
    }
}

This loop is suppose to break when 999 is entered, but when entered before breaking it output error message from the else inside the loop. It's not suppose to output anything before breaking. we tried moving the while part of the loop, but it did not affect anything. We can't see any other problems with it.

Your do-while is working correctly - the course of action is the following:

在此处输入图片说明

  1. In a do-while , the statements are always executed at least once. So you enter the do { } , and fall into the else condition, since 999 is greater than 0 and not smaller than 100.
  2. You then evaluate the expression grade != 999 -> this is false, since grade == 999 .
  3. You don't do the do { } again and come out of the do-while .

To achieve the behavior that you want, you will need to add an additional statement inside the do { } , eg:

...
if (grade == 999) {
   break; //or print statement
}
else if (grade >= 0 && grade <= 100) {
...

You could wrapp the message by another if that checks if the number is not 999.

else
{
    if (grade != 999) {
        System.out.println("Incorrect value, please reenter grade:");
    }
}

You could check input value for grade == 999 and exist in case it equals to:

public static double getAverage() {
    try (Scanner scan = new Scanner(System.in)) {
        int total = 0;
        int sum = 0;
        int average = 0;

        while (true) {
            System.out.print("Please enter a grade b/w 0-100 (or 999 to exit): ");
            int grade = scan.nextInt();

            if (grade >= 0 && grade <= 100) {
                total++;
                sum += grade;
            } else if (grade == 999)
                break;
            else
                System.out.println("Incorrect value\n");
        }

        return (double)sum / total;
    }
}

Change else to else if (grade != 999) when you are displaying the error.

Do it as follows:

import java.util.Scanner;

public class Main {
    private static int grade, total, sum, average;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        do {
            System.out.println("Please enter a grade b/w 0-100:");
            grade = sc.nextInt();
            if (grade >= 0 && grade <= 100) {
                total++;
                sum = sum + grade;
                System.out.println("Please enter another grade or 999 to break:");
            } else if (grade != 999) {
                System.out.println("Incorrect value, please reenter grade:");
            }
        } while (grade != 999);
        average = sum / total;
    }
}

A sample run:

Please enter a grade b/w 0-100:
24
Please enter another grade or 999 to break:
Please enter a grade b/w 0-100:
999

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