我目前正在尝试制作一个打开,关闭,打开和关闭“洗碗机”的程序。当我运行这个程序一次时,我得到了我想要的结果:
Welcome to SiriWash
What would you like to do?
Open Door | Close Door | Turn on | Turn Off
open door
opening door
The Dishwasher is Open and Off
What would you like to do?
Open Door | Close Door | Turn on | Turn Off
如果我再次这样做,我会得到:
Welcome to SiriWash
What would you like to do?
Open Door | Close Door | Turn on | Turn Off
open door
opening door
The Dishwasher is Open and Off
What would you like to do?
Open Door | Close Door | Turn on | Turn Off
close door
What would you like to do?
Open Door | Close Door | Turn on | Turn Off
这是Runner Code:
import java.util.Scanner;
public class Runner {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
boolean gameRunning = true;
Dishwasher washer1 = new Dishwasher(false,false);
washer1.open();
washer1.turnOff();
//System.out.println(washer1);
System.out.println("Welcome to SiriWash");
String answer = "";
//String answer = "";
while (gameRunning) {
System.out.println("What would you like to do?");
System.out.println("Open Door | Close Door | Turn on | Turn Off ");
answer = reader.nextLine();
if (answer.equalsIgnoreCase("open door")) {
washer1.open();
System.out.println("opening door");
System.out.println(washer1);
answer = "";
if (answer.equalsIgnoreCase("close door")) {
washer1.close();
System.out.println("Closing door");
System.out.println(washer1);
answer = "";
if (answer.equalsIgnoreCase("turn on")) {
washer1.turnOn();
System.out.println("turning on");
System.out.println(washer1);
answer = "";
if (answer.equalsIgnoreCase("turn off")){
washer1.turnOff();
System.out.println("turning off");
System.out.println(washer1);
answer = "";
}
}
}
}
}
}
}
似乎在第二次输入时会跳过if语句,我该如何解决?
答案 0 :(得分:0)
所有if
都是嵌套的,所以如果第一次失败,则会跳过所有其余的if
。尝试在每个if( condition1 ){
body
}
if( condition2 ){
body2
}
...
主体之后关闭花括号:
{{1}}