我是编码的新手(大约一个半月半上高中comp sci课),并且一直无法使我的代码生效。我已经尝试了所有可以想到的方法,但无法使其正常工作。我的代码的相关部分在这里。
public void input() {
Scanner input = new Scanner(System.in);
userInput = input.nextLine();
}
public void openingScene() {
scenarioLeft = "forest";
scenarioRight = "desert";
System.out.println("You are in a forest, surrounded by trees. To
your left is a patch of trees. To your right is a desert. (left/right)");
}
public void currentLoc() {
if(userInput == "left") {
location = scenarioLeft;
}else if(userInput == "right"){
location = scenarioRight;
}
}
public void game() {
input();
currentLoc();
System.out.println(location);
}
(openingScene()由另一种方法调用,但在其他所有方法之前被调用) 我一直在尝试将变量位置设置为正确的值,但是没有运气。我知道scenarioLeft和scenarioRight(两个字符串)都已正确设置,userInput(也是一个String)也已正确设置。 (注意-所有这些变量都在类的早期设置,并且都在构造函数中设置)。它为位置打印的所有内容都是在构造函数中设置的内容。我已经为此困扰了几天,任何帮助将不胜感激!