Как можно попросить пользователя по-английски чтобы он ввёл таки Yes или No?
Простая текстовая игра задаёт игроку закрытый вопрос, на который ожидается ответ Yes или No. Если пользователь вводит что-то другое, программа должна переспросить. Так как это игра, то отличной идеей будет переспрашивать игрока используя разыные фразы, а не с помощью скучного "Please enter Yes or No".
Я закодировал алгоритм на Java, но обнаружил, что мне не так просто придумать разные фразы. Если вы знаете подобные фразы, напишите, пожалуйста!
Ниже фрагмент кода с выбором фразы.
private void sayGoodbye() {
System.out.println();
System.out.println(pickMessage(new String[]{
"Bye!",
"Bye, bye!",
"See you later!",
"See you soon!",
"Talk to you later!",
"I’m off!",
"It was nice seeing you!",
"See ya!",
"See you later, alligator!",
"In a while, crocodile!",
"Have a nice one!"
}));
}
private boolean askYesNo() {
while (true) {
final var answer = scanner.nextLine().toUpperCase().charAt(0);
if (answer == 'Y' || answer == 'N') {
return answer == 'Y';
}
System.out.println(pickMessage(new String[]{
"Come on, yes or no?",
"Please enter yes or no."
}));
}
}
private String pickMessage(final String[] messages) {
return messages[random.nextInt(messages.length)];
}
Ответы (1 шт):
Автор решения: Jegors Čemisovs
→ Ссылка
Пока нашёл такие решения (фразы).
public boolean askYesNo() {
while (true) {
final var answer = scanner.nextLine().toUpperCase().charAt(0);
if (answer == 'Y' || answer == 'N') {
return answer == 'Y';
}
System.out.println(pickMessage(new String[]{
"Come on, yes or no?",
"Please enter yes or no.",
"Funny, I still don't understand, is it yes or no?",
"Sorry, it must be yes or no.",
"Let's try again: yes or no?",
"I'm not sure I caught you: was it yes or no?",
"Oh, it's too complicated for me: just say me yes or no.",
"I'm filling a bit intrigued: just say yes or no.",
"I am a bit confused, give me a simple answer: yes or no",
"Oh, no, don't try to confuse me: say yes or no.",
"Could you please simply say yes or no?",
"Sorry, may l ask you again: was it yes or no?"
}));
}
}