我不知道为什么这个java代码没有';行不通
好吧,我做了一个逃逸房间的程序,光的布尔值不起作用我不知道为什么这个java代码没有';行不通,java,operators,Java,Operators,好吧,我做了一个逃逸房间的程序,光的布尔值不起作用 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 Sc
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);
}
您想使用
==
,而不是=
。另外,考虑USER回答可能是NULL,所以您应该做的是代码>“A”。
而不是:
if (useranswer.equals("A")&&(light = false)){
ceilingCloserlightFalse(sc, useranswer, inventory, light);
}
把你的比较写得像
if (useranswer.equals("A")&&(false = light)){
ceilingCloserlightFalse(sc, useranswer, inventory, light);
}
然后会出现一个编译器错误,指出您使用的是
=
而不是=
light=false
,您的意思可能是=
。而且,usernaswer
看起来像是打字错误。很难说为什么它不起作用,因为你没有说它应该做什么,也没有说它应该做什么。