SP de Wit
SP de Wit

Reputation: 215

I cant get out of my while loop

The following program i wrote for a class activity to prompt the user for a infinite amount of test marks between 0 - 100 and when a user enters a mark outside that range it should tell the user that its invalid(which works so far in my program). When the user enters "-1" its should stop the program and then print out the average of those marks.

import java.util.*; 

public class HmWk62 {

    static Scanner console = new Scanner(System.in);

    public static void main(String[] args) {


        int count=1; 
        int mark, total=0; 
        double average; 

        System.out.println("please enter mark "+count);
        mark = console.nextInt();
        count++; 

        while ((mark >= 0)&&(mark <= 100)){
            System.out.println("Please enter mark "+count);
            mark = console.nextInt(); 
            total += mark ;
            ++count; 

            while ((mark < 0)||(mark > 100)){
                System.out.println("Invalid mark, please re-enter");
                mark = console.nextInt(); 
            }
        }
    }
}  

Upvotes: 0

Views: 135

Answers (4)

Gijsbert Brouwer
Gijsbert Brouwer

Reputation: 698

Let your code check if a global variable is set.

For instance, create the variable: boolean userCancelled = false;

in the while loop check this variable: like:

while (userCancelled  != false && (mark >= 0)&&(mark <= 100))
{
 }

Upvotes: 0

Blip
Blip

Reputation: 3171

I suggest that you replace the while loop with this do while loop as below:

    do {
        System.out.println("Please enter mark "+count);
        mark = console.nextInt(); 
        if(mark == -1){
           break;
        }else if((mark >= 0)&&(mark <= 100)){
           total += mark ;
           ++count; 
        }else{
            System.out.println("Invalid mark, please re-enter");
            mark = 0; 
        }
    } while ((mark >= 0)&&(mark <= 100));

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499860

When the user enters "-1" its should stop the program and then print out the average of those marks.

Well if the user enters -1, you'll never get out of the nested loop that validates the input.

If you want to allow -1, you should changed the condition in the nested loop to allow it:

while (mark < -1 || mark > 100)

Also note that you're using the value of mark before you validate it - so if you enter 10000, you'll still add 10000 to total before you then ask for a new value - and you'll then ignore the new value.

Also, you're not using the first value of mark entered at all, other than to see whether or not you should enter the loop.

I suspect you actually want something like:

while (true) {
    int mark = readMark(scanner, count);
    if (mark == -1) {
        break;
    }
    count++;
    total += mark;
}
// Now print out the average, etc

...

private static int readMark(Scanner scanner, int count) {
    System.out.println("Please enter mark " + count);
    while (true) {
        int mark = scanner.nextInt();
        if (mark >= -1 && mark <= 100) {
            return mark;
        }
        System.out.println("Invalid mark, please re-enter");
    }
}

Upvotes: 6

NickJ
NickJ

Reputation: 9559

The inner while-loop is then one you're stuck in.

On the one hand, you need an invalid mark to exit, but on the other, the program rejects invalid marks and keeps on asking until you enter a valid mark.

Result - you're stuck.

Solution: Change the inner while to an if, and place the average calculation code and a System.exit() in the if block.

Upvotes: -1

Related Questions