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;
}
}
}
boolean continueLooping = true;while (continueLooping) {...} Also, avoid toggles and set values explicitly: i.e.continueLooping = false;`