重复"readInt"直到输入错误

repeat "readInt" till the input is wrong

我想解决一个小问题,但对我来说却是个大问题。

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

A​​CM 库:

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;
}

如果用户键入例如“-1”,程序将提示他再次输入。这个不错。
但是第二个输入(例如“2”)不会中断 while 循环。

您必须将第二个 readInt 分配给一个变量,例如:

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