Search code examples
javarepeatbreakacm-java-libraries

repeat "readInt" till the input is wrong


I want to solve a small problem, but for me it's a big one.

"This program should start by asking the user for N; if N is outside of the desired range, the user should be asked again."

ACM library:

int N = readInt("Enter N (0 <= N <= 10): ");

while (N < 0 ^ N > 10) {
  readInt("Enter N (0 <= N <= 10): ");
  if(N > 0 && N < 11) break;
}

If the User typed for example "-1", the program prompts him for input again. This is good.
But the second input (for example "2") doesn't break the while loop.


Solution

  • You must assign your second readInt to a variable like:

    N = readInt("Enter N (0 <= N <= 10): ");