英文:
How to re-ask the user to enter Yes or No?
问题
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[]{
"C'mon, make a choice: yes or no?",
"Don't leave me hanging! yes or no?",
"Time to shine! Type yes or no.",
"Ready to roll? Tell me yes or no!",
"Give me some love! Yay or nay?"
}));
}
}
private String pickMessage(final String[] messages) {
return messages[random.nextInt(messages.length)];
}
英文:
A simple text game expects input from the user in a YES or NO format.
If the format is incorrect, the program should ask again.
I want the game to use different funny phrases instead of boring ones like "Please enter yes or no".
I know how to code it in Java but English is not my native language and I find it difficult to come up with such phrases. I would be glad if you can help me come up with these phrases in English. May be some ideas where I can find such phrases.
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
得分: 2
首先,让我们修复您现有代码中的一些错误:
- 您缺少一个 else 块,这意味着 if 块后面的代码将始终执行;
- 不管输入是
Y
还是N
,您都在赋值answer == 'Y';
并返回它; - 返回未转换的(类型未推断)类型,预期应该是一个
Character
(所以,您应该进行转换),但您的方法声明要返回完全不同的类型 -boolean
。
现在让我们从您的数组中选择一个随机的字符串元素,同时修复现有的问题:
private Character askYesNo() {
while (true) {
final char answer = scanner.nextLine().toUpperCase().charAt(0);
if (answer == 'Y' || answer == 'N') {
return answer;
} else {
pickMessage(messages); // 假设您有一个名为 messages 的变量
};
}
}
private String pickMessage(final String[] messages) {
Random random = new Random();
return messages[random.nextInt(messages.length)];
}
这是修复后的代码。
英文:
First of all, let's fix the few mistakes in your existing code:
- You are missing an else block, which means, that lines after the if block will always execute;
- Disregarding of whether the input is
Y
orN
, you're assigninganswer == 'Y';
and returning that; - Returning uncasted (type is not inferred) type, which is expected to be a
Character
(so, you should cast it), yet your method declares to be returning completely different type -boolean
.
Now let's select a random String element from your array, by also fixing existing problems:
private Character askYesNo() {
while (true) {
final char answer = scanner.nextLine().toUpperCase().charAt(0);
if (answer == 'Y' || answer == 'N') {
return answer;
} else {
pickMessage(messages); //assuming that you have a messages variable
};
}
}
private String pickMessage(final String[] messages) {
Random random = new Random();
return messages[random.nextInt(messages.length)];
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论