【发布时间】:2014-10-14 18:47:51
【问题描述】:
我对@987654321@ 的使用有疑问。在我的代码中,用户将通过输入true 或false 来回答问题。然后字符串应该被转换为boolean。
public String setCorrespondingAuthor ()
{
String respons = "true";
do
{
System.out.print("Is s/he the corresponding author? (true/false)");
respons = sc.next();
sc.nextLine();
} while (!respons.equalsIgnoreCase("true")
&& !respons.equalsIgnoreCase("false"));
boolean flag = Boolean.valueOf(respons);
if (!flag)
{
return "not the corresponding author";
}
return "the corresponding author";
}
现在,它工作正常。问题是在输出中,它在处理之前提示了两次问题。
【问题讨论】:
-
这可能不是您使用
Boolean.valueOf的问题。我认为您的输入处理有问题。首先输入“true”然后输入“false”来测试。你的输出是“真”还是“假”的输出? -
@trw 该条件基本上是“当响应不等于 true 也不等于 false”。
-
sc.nextLine();立即请求第二个输入,不是吗? -
正如@Arkanon 所说,错误在您的
do/while -
@Freiheit:当我输入“true”然后“false”时,我得到了答案,因为输出为“false”(来自第二行)。