如果我需要两个选项,如何正确使用布尔方法?
问题是,如果我只需要从布尔方法中的两个选项(是或否)中进行选择,如何将其放入 IF 中? 我尝试这样做(见下文),它强调了最后一个大括号。如果我在 while 之外使用默认 return (但我不想),它会强调第一个 return (在第一个 if 之后)。
static boolean isAnotherGamer() {
System.out.println("Play another game? Type in Y or N");
Scanner scanner = new Scanner(System.in);
String answer = scanner.nextLine();
while (true) {
if (answer.equalsIgnoreCase("Y")) {
break;
return true;
} else if (answer.equalsIgnoreCase("N")) {
break;
return false;
}
System.out.println("Input mismatch");
} //IDE underline this brace
}
The question is, if I need to chose only from two options in boolean method (Yes or No) how do I put it in IFs?
I try to do like this (see below), it underlines very last brace. If I use default return outside while (but I don't want to), it underlines first return (after first if).
static boolean isAnotherGamer() {
System.out.println("Play another game? Type in Y or N");
Scanner scanner = new Scanner(System.in);
String answer = scanner.nextLine();
while (true) {
if (answer.equalsIgnoreCase("Y")) {
break;
return true;
} else if (answer.equalsIgnoreCase("N")) {
break;
return false;
}
System.out.println("Input mismatch");
} //IDE underline this brace
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我将这样做。这允许输入
yes
或no
的任何部分。我认为最好传递一个 Scanner 实例,而不是每次都创建一个。使用正则表达式可以让答案有一定的自由度。^$
- 字符串的开头和结尾。(?i)
- 忽略大小写ye?s?
- 表示必须有y
但e
和s
是可选的。Here is how I would do it. This allows any part of
yes
orno
to be entered. I think it best to pass a Scanner instance rather than creating one each time. Using a regular expression allows for some latitude in the answer.^$
- beginning and end of string.(?i)
- ignore caseye?s?
- says must havey
bute
ands
are optional.为什么不能先验证输入,然后在输入是或否后,再决定要做什么。如果不是,您可以使重复语句继续运行,直到获得所需的内容。 return 语句的位置是问题所在,因为如果 if 或 else if 语句不为 true,则该方法将不会像您的方法签名所暗示的那样返回布尔值,并且您的方法将只是一个无限循环。
Why can you not validate the input first, and then after the input is either a yes or no, decide on what to do. If it is not either, you can make the repetition statement continue to run until after you get what you need. The location of your return statement is the problem because if either if or else if statements are not true, the method will not return a boolean as your method signature suggests, and your method will just be an infinite loop.
您的方法被声明返回一个布尔值。流程中没有 return 语句。
假设您进入无限循环。此时我们评估用户输入的内容(为什么我们要在无限循环中这样做?答案在中间不会改变,不是吗?)
如果是“y”,我们就中断循环。
如果是“n”,我们就打破循环。
在任何其他情况下,我们都会打印一些内容并保留在循环中。
但一旦循环被打破 ->返回语句在哪里?
所以从我的角度来看,该函数应该如下所示:
Your method is declared to return a boolean. There is no return statement in the flow.
Assume you go into the endless loop. At this moment we evaluate what the user entered (why do we do that inside the endless loop? The answer does not change inbetween, does it?)
If it is 'y', we break the loop.
If it is 'n', we break the loop.
In any other case we print something and remain in the loop.
But as soon as the loop was broken -> where is the return statement?
So from my POV, the function should look like this:
因为您尚未设置默认返回值,所以如果用户不选择“Y”或“N”,则不会返回任何内容,这就是您收到错误的原因。
此外,您不应该在break语句之后放置任何代码,因为这些行将被完全忽略(同样,由于return语句在break之后,所以不会返回任何内容。)
如果您只想完全删除这些break语句,则可以完全删除这些break语句一旦获得布尔值就退出该方法,或者如果您想继续在方法内运行代码,则可以更新布尔变量以供将来使用。 (我已经提供了一个例子)
Because you've not set a default return value, if the user doesn't choose either "Y" or "N" then nothing is going to be returned so that's why you're getting an error.
Additionally, you shouldn't be putting any code after your break statements as those lines will be completely ignored (again, nothing returned as your return statements are after your breaks.)
You can just completely remove those break statements if you're just wanting to quit that method once you've got your boolean value or you can update a boolean variable for future use if you're wanting to keep running code inside your method. (I've provided an example of this)