我无法弄清楚为什么我的代码无效。
如果不等于是或否区域,它似乎正在打破。
这是我的代码:
public static void main(String[] args) {
// TODO code application logic here
Scanner user_input = new Scanner(System.in);
String name;
System.out.println("Hello, what is your name?");
name = user_input.next();
System.out.println("");
String name_answer;
System.out.println("Your name is " + name + ". Is this correct? (yes/no)");
name_answer = user_input.next();
System.out.println("");
if (!name_answer.equals("yes" + "no")) {
System.out.println("Answer not valid. Please input again.");
name_answer = user_input.next();
while (!name_answer.equals("yes" + "no")) {
System.out.println("Answer not valid. Please input again.");
name_answer = user_input.next(); } }
if (name_answer.equals("yes")) {
System.out.println("Thank you, " + name + ". Please proceed to the next question.");
} else if (name_answer.equals("no")) {
System.out.println("Please reinput your name correctly.");
while (name_answer.equals("no")) {
String name_again;
System.out.println("");
System.out.println("What is your correct name?");
name_again = user_input.next();
System.out.println("");
System.out.println("Your name is " + name_again + ". Is this correct? (yes/no)");
name_answer = user_input.next(); }
如果我注释掉了not-equals代码块(如下所示),程序就可以运行了。但是,随着代码块的进入,程序会中断。
if (!name_answer.equals("yes" + "no")) {
System.out.println("Answer not valid. Please input again.");
name_answer = user_input.next();
while (!name_answer.equals("yes" + "no")) {
System.out.println("Answer not valid. Please input again.");
name_answer = user_input.next(); } }
我的目标是让任何答案不等于“是”或“否”重新输入,而“是”或“否”将程序带到另一个步骤。谢谢你的帮助。
答案 0 :(得分:2)
您的代码的技术问题是您使用连接而不是逻辑运算符。 SELECT
left(name,length(name) - strpos(reverse(name),' ')) as which
,COUNT(*)
FROM dataframe
GROUP BY
left(name,length(name) - strpos(reverse(name),' '))
评估为"yes" + "no"
,这可能永远不会与您的输入字符串匹配。
更重要的是,问题在于您尝试将两个布尔评估捆绑为一个。从逻辑上讲,如果答案不是"是"那么你想继续。 和答案不是"不是"。在Java语法中:
"yesno"
如果要一次测试多个值,可以使用以下快捷方式:
if (!name_answer.equals("yes") && !name_answer.equals("no")) {