我不知道为什么这段 Java 代码不起作用。

huangapple go评论85阅读模式
英文:

I have no idea why this java code doesn't work

问题


class Main {
  public static void main(String[] args) {
    System.out.println("欢迎来到霍格沃茨逃离密室游戏");
    System.out.println("要玩霍格沃茨逃离密室游戏,请按Y");
    Scanner sc = new Scanner(System.in);
    String useranswer = sc.nextLine();
    boolean light = false;

    System.out.println("按U抬头看");
    useranswer = sc.nextLine();
    if (useranswer.equals("U")) {
      System.out.println("你抬头看去,但由于天花板太暗,什么也看不清。");
      System.out.println("你想要:\n仔细观察,按A\n返回原位置,按B");
      useranswer = sc.nextLine();
      System.out.println(light);
      if (useranswer.equals("A") && !light) {
        ceilingCloserlightFalse(sc, useranswer, light);
      } else if (useranswer.equals("B")) {
        original(sc, useranswer, light);
      }
    }
  }

  public static void ceilingCloserlightFalse(Scanner sc, String useranswer, boolean light) {
    System.out.println("你靠近一些,隐约看到一些东西,但因为没有光线,无法判断是什么。");
    System.out.println("由于那里没有其他事情可做,你返回了原来的位置。");
    original(sc, useranswer, light);
  }

  public static void original(Scanner sc, String useranswer, boolean light) {
    // 在这里编写返回原位置后的操作
  }
}

如果你遇到问题,可能是因为代码中有几个拼写错误和逻辑错误。我进行了一些修正,但这只是一部分代码,还有可能有其他问题。如果问题仍然存在,你可能需要进一步检查代码的其他部分。

英文:

Ok SO I made an escape the room program and the boolean for light 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);
    
  }
    
  }
}

Ok So I wonder if I am doing something wrong with the method or the && operator. I checked to make sure that useranswer was a and I also checked to make sure that

答案1

得分: 1

这里你正在将 false 赋值给变量,而不是进行比较:

if (useranswer.equals("A") && (light = false)){
      ceilingCloserlightFalse(sc, useranswer, inventory, light);
    }

你应该使用 ==,而不是 =。另外,要考虑 useranswer 可能为空,所以应该使用 "A".equals(useranswer) 来避免空指针异常。

英文:

Here you are assigning false to the variable, not comparing it:

if (useranswer.equals("A")&&(light = false)){
      ceilingCloserlightFalse(sc, useranswer, inventory, light);
    }

You want to use ==, not =. Also, consider that useranswer might be null, so you should be doing "A".equals(useranswer) to avoid a NullPointerException

答案2

得分: 1

有些开发者使用 Yoda 语法来避免我们这里常见的错误。

与其写成:

if (useranswer.equals("A") && (light = false)){
      ceilingCloserlightFalse(sc, useranswer, inventory, light);
}

可以将比较方式颠倒,写成:

if (useranswer.equals("A") && (false = light)){
      ceilingCloserlightFalse(sc, useranswer, inventory, light);
}

这样,编译器会报错,指出你使用了 = 而不是 ==

英文:

Some developers use Yoda speak to avoid the common mistake we have here.

Instead of:

if (useranswer.equals("A")&&(light = false)){
      ceilingCloserlightFalse(sc, useranswer, inventory, light);
    }

Write your comparison backward like

if (useranswer.equals("A")&&(false = light)){
      ceilingCloserlightFalse(sc, useranswer, inventory, light);
}

Then you'd get a compiler error pointing you to the fact you're using = instead of ==.

huangapple
  • 本文由 发表于 2020年8月24日 22:53:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63563459.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定