StatefulSet, ReplicaSet или DaemonSet. Что лучше для одного стручка?

Переменные флагов слишком подвержены ошибкам для использования. Вместо этого используйте явный контроль цикла с комментариями. Кроме того, hasNextInt() не блокируется. Это неблокирующая проверка, чтобы увидеть, может ли будущий вызов next получить вход без блокировки. Если вы хотите заблокировать, используйте метод nextInt() .

// Scanner that will read the integer
final Scanner in = new Scanner(System.in);
int inputInt;
do {  // Loop until we have correct input
    System.out.print("Specify an integer between 0 and 5: ");
    try {
        inputInt = in.nextInt(); // Blocks for user input
        if (inputInt >= 0 && inputInt <= 5)  { 
            break;    // Got valid input, stop looping
        } else {
            System.out.println("You have not entered a number between 0 and 5. Try again.");
            continue; // restart loop, wrong number
         }
    } catch (final InputMismatchException e) {
        System.out.println("You have entered an invalid input. Try again.");
        in.next();    // discard non-int input
        continue;     // restart loop, didn't get an integer input
    }
} while (true);

1
задан Nurza 18 January 2019 в 15:48
поделиться