我对Java相当陌生,正在尝试创建一个包含转义的文本游戏。这里是我的代码:虽然循环和Java中的问题
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
public class Adventure {
public static void main (String args[]) throws InterruptedException{
System.out.println("YOU WAKE ON YOUR COUCH. YOU NEED TO GET OUT OF YOUR HOUSE.");
int x = 1;
while (x == 1){
Scanner ACTION = new Scanner(System.in);
System.out.println("WHAT DO YOU DO NEXT?");
String action;
action = ACTION.next();
if (action == "a"){
System.out.println("if");
}else{
System.out.println("else");
}
}
}
}
尝试运行,并看到,如果不工作,如果你输入“A”,它打印“其他”。请在你的回答中解释每一行新的代码(如果有的话)。谢谢。
你的问题是什么? –
你的while循环不会停止。你不会改变x的值。你应该使用equals来比较字符串。 –
'=='检查两个字符串是否字面上是相同的字符串(认为同一张纸),而'string.equals(otherString)'检查它们是否有相同的字母(可能写在不同的纸上) –