我不知道为什么这个 java 代码不起作用
I have no idea why this java code doesn't work
好的,所以我做了一个逃离房间的程序,但是灯光的布尔值不起作用
class Main {
public static void main(String[] args) {
System.out.println("Welcome To The Hogwarts Escape Room");
System.out.println("To Play The Hogwarts Escape Room Press Y");
Scanner sc = new Scanner(System.in);
String useranswer = sc.nextLine();
boolean light = false;
// This is not my entire code but the part that was necessary.
System.out.println("Press U to look up");
useranswer = sc.nextLine();
if (usernaswer.equals("U"){
System.out.println(" You look above you and see nothing as the ceiling is too dark to see in.");
System.out.println("Would you like to: \n Look Closer press A \n Return To Your Original Position press B");
useranswer = sc.nextLine();
System.out.println(light);
// this part doesn't work.. I checked and made sure the useranswer was A and the boolean does equal to false.
if (useranswer.equals("A")&&(light = false)){
ceilingCloserlightFalse(sc, useranswer, inventory, light);
}
else if (useranswer.equals("B")){
original(sc, useranswer, inventory, light);
}
// this was the setup to the constructor
public static void ceilingCloserlightFalse(Scanner sc, String useranswer, String [] inventory, boolean light){
System.out.println("You go closer and see something faintly but cant tell what it is without a light");
System.out.println("You returned to the original position because there was nothing else to do there.");
original(sc, useranswer, inventory, light);
}
}
}
好的所以我想知道我是否在方法或 && 运算符上做错了什么。我检查以确保 useranswer 是 a 并且我还检查以确保
在这里你将 false 赋值给变量,而不是比较它:
if (useranswer.equals("A")&&(light = false)){
ceilingCloserlightFalse(sc, useranswer, inventory, light);
}
您想使用 ==
,而不是 =
。此外,考虑到 useranswer 可能为 null,因此您应该 "A".equals(useranswer)
以避免 NullPointerException
一些开发人员使用 Yoda speak 来避免我们在这里遇到的常见错误。
而不是:
if (useranswer.equals("A")&&(light = false)){
ceilingCloserlightFalse(sc, useranswer, inventory, light);
}
像这样向后写比较
if (useranswer.equals("A")&&(false = light)){
ceilingCloserlightFalse(sc, useranswer, inventory, light);
}
然后你会得到一个编译器错误,指出你使用的是 =
而不是 ==
。
好的,所以我做了一个逃离房间的程序,但是灯光的布尔值不起作用
class Main {
public static void main(String[] args) {
System.out.println("Welcome To The Hogwarts Escape Room");
System.out.println("To Play The Hogwarts Escape Room Press Y");
Scanner sc = new Scanner(System.in);
String useranswer = sc.nextLine();
boolean light = false;
// This is not my entire code but the part that was necessary.
System.out.println("Press U to look up");
useranswer = sc.nextLine();
if (usernaswer.equals("U"){
System.out.println(" You look above you and see nothing as the ceiling is too dark to see in.");
System.out.println("Would you like to: \n Look Closer press A \n Return To Your Original Position press B");
useranswer = sc.nextLine();
System.out.println(light);
// this part doesn't work.. I checked and made sure the useranswer was A and the boolean does equal to false.
if (useranswer.equals("A")&&(light = false)){
ceilingCloserlightFalse(sc, useranswer, inventory, light);
}
else if (useranswer.equals("B")){
original(sc, useranswer, inventory, light);
}
// this was the setup to the constructor
public static void ceilingCloserlightFalse(Scanner sc, String useranswer, String [] inventory, boolean light){
System.out.println("You go closer and see something faintly but cant tell what it is without a light");
System.out.println("You returned to the original position because there was nothing else to do there.");
original(sc, useranswer, inventory, light);
}
}
}
好的所以我想知道我是否在方法或 && 运算符上做错了什么。我检查以确保 useranswer 是 a 并且我还检查以确保
在这里你将 false 赋值给变量,而不是比较它:
if (useranswer.equals("A")&&(light = false)){
ceilingCloserlightFalse(sc, useranswer, inventory, light);
}
您想使用 ==
,而不是 =
。此外,考虑到 useranswer 可能为 null,因此您应该 "A".equals(useranswer)
以避免 NullPointerException
一些开发人员使用 Yoda speak 来避免我们在这里遇到的常见错误。
而不是:
if (useranswer.equals("A")&&(light = false)){
ceilingCloserlightFalse(sc, useranswer, inventory, light);
}
像这样向后写比较
if (useranswer.equals("A")&&(false = light)){
ceilingCloserlightFalse(sc, useranswer, inventory, light);
}
然后你会得到一个编译器错误,指出你使用的是 =
而不是 ==
。