所以即時嘗試製作一個爲學校提出兩個問題的程序。我所擁有的問題是,當我只想要一個時,java會一直打印多條消息。是any
一個能夠告訴我如何做到這一點的權利?下面是代碼:如何讓java只用if語句打印一條消息
if (answer1.equals("inside") ||
answer1.equals("outside") ||
answer1.equals("both"))
System.out.println("Question 2) Is it a living thing?(yes/no)");
answer2 = keyboard.next();
if (answer2.equals("yes"))
System.out.println("Then what else could you be thinking of besides a houseplant?!");
if (answer2.equals("no"))
System.out.println("Then what else could you be thinking of besides a shower curtain?!");
if (answer2.equals("yes"))
System.out.println("Then what else could you be thinking of besides a bison?!");
if (answer2.equals("no"))
System.out.println("Then what else could you be thinking of besides a billboard?!");
if (answer2.equals("yes"))
System.out.println("Then what else could you be thinking of besides a dog?!");
if (answer2.equals("no"))
System.out.println("Then what else could you be thinking of besides a cell phone?!");
}
}
我不知道我的理解,你總是檢查_same_條件。你想在這裏做什麼? – Tunaki
我試圖做的是讓每個語句打印一個不同的答案,我真的不知道如何做到這一點,而不使用其他,否則如果或嵌套if語句(這是作業的一部分) – jackmasterlooter
'answer2'總是與' 「yes」或「no」,所以如果以前是真的,它將保持爲真,因爲變量沒有改變值。打印不同答案的條件是什麼? – Tunaki