0

Around here - if (code<10 || code>99) seems to be the issue. When entering a number outside the range the loop continues infinitely, seemingly ignoring the condition. I tried System.exit(0), though that did the job I would like to try and use the while loop to stop the code.

import java.util.*;
public class LockPicker {

    public static void main(String[] args) {
        Scanner kb = new Scanner(System.in);
        Random r = new Random();
        boolean stop = false; 
        while (!stop) {
            System.out.print("What is the unlock code? ");
            int code = kb.nextInt();
            if (code<10 || code>99) {
                System.out.println("Your number must be between 10 and 99");
                stop = !stop;
            }

            System.out.println("Picking the lock...");
            System.out.println("");
            int x = -1, counter = 0;
            while (x!=code) {
                x = r.nextInt(90)+10;
                System.out.println(x);
                counter++;
            }
            System.out.println("That took only "+counter+" tries to pick the lock!");
            stop = !stop;
        }
    }
}
2
  • 2
    Your if statement executes properly, and sets stop to true, but then execution continues, and the last statement within the body of the loop reverses the value of stop, setting it back to false. Commented Nov 12, 2014 at 20:03
  • 1
    I always find curious that newbie developers have problems with negating a boolean used in a break condition. My suggestion to new developers is to try to express things in positive logic. For example: boolean continueLooping = true; while (continueLooping) {...} Also, avoid toggles and set values explicitly: i.e. continueLooping = false;` Commented Nov 12, 2014 at 20:09

2 Answers 2

3

You don't need the stop variable. You can use break and continue.

Random r = new Random();
while (true) {
    System.out.print("What is the unlock code? ");
    int code = kb.nextInt();
    if (code < 10 || code > 99) {
        System.out.println("Your number must be between 10 and 99");
        continue;
    }

    System.out.println("Picking the lock...");
    System.out.println("");
    int x = -1, counter = 0;
    while (x != code) {
        x = r.nextInt(90) + 10;
        System.out.println(x);
        counter++;
    }
    System.out.println("That took only " + counter
            + " tries to pick the lock!");
    break;
}

continue will just skip the iteration and go to the while loop to prompt the user again for a number. break will end the while loop when a match is found.

Sign up to request clarification or add additional context in comments.

Comments

0

You're toggling stop twice in the case in which your number is outside the desired range. That makes your loop run forever. You really want to set stop to true, not toggle it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.