Проблема при использовании цикла while для проверки данных - PullRequest
0 голосов
/ 07 апреля 2019

Я объявил сканер, запуск

System.out.println();
System.out.println("Type the word \"Start\" when your ready to begin the quiz");
start = input.nextLine();
input.nextLine();
while(!start.equals("start")){
  System.out.println("Make sure you read the question and try again");
  start = input.nextLine();
}

Я ожидал ввести «старт», и цикл while завершится, но вместо этого мне нужно дважды ввести «старт», чтобы выйти из цикла while

1 Ответ

0 голосов
/ 07 апреля 2019

Вы можете использовать цикл do - while.Он отличается от цикла while тем, что сначала выполняет код, а затем оценивает условие, заданное в операторе while.

    System.out.println();
    System.out.println("Type the word \"start\" when your ready to begin the quiz");
    String start, answer;
    Scanner input = new Scanner(System.in);
    do {
        start = input.nextLine();
        if(!start.equals("start")) {
            System.out.println("Make sure you read the question and try again");
        } else {
            System.out.println("Enter the answer here: ");
            answer = input.nextLine();
        }
    } while (!start.equals("start"));
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...