【问题标题】:Why is the if statement not working after random boolean?为什么 if 语句在随机布尔值之后不起作用?
【发布时间】:2015-09-06 19:53:10
【问题描述】:

为什么if 语句行出错?我试图在随机布尔值之后获得if 语句。这可能吗?

package lieDetector;

import java.util.Random;
import java.util.Scanner;

public class LieDetector {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        System.out.println("Type In a Question here");
        String q1 = scanner.nextLine();

        System.out.println("Insert Answer here");
        String a1 = scanner.nextLine();

    }
    public boolean getRandomBoolean() {
        Random random = new Random();
        return random.nextBoolean();
        if (random.boolean == true);
        System.out.println("you are telling the truth");
    }
}

【问题讨论】:

  • return random.nextBoolean();?
  • return random.nextBoolean(); 这将在此处结束方法调用...
  • 而且这段代码也不会编译
  • 您遇到了一个错误,因为您编写了永远无法调用的代码。所以你会得到一个“无法访问的代码”的错误

标签: java if-statement boolean


【解决方案1】:

为什么 if 语句行出错?

因为Random 没有提供名为boolean 的字段。 (random.boolean...)

我试图在一个随机布尔值之后得到一个 if 语句。这可能吗?

是的,但您需要正确的语法。

return random.nextBoolean();

这一行将结束您的方法调用。同一方法中直接位于其下方的任何代码都不会执行。

public boolean getRandomBoolean() {
    Random random = new Random();
    boolean myRandomBoolean = random.nextBoolean(); // instead, assign the next random boolean to a variable
    if (myRandomBoolean) { // remove the semicolon and replace with {
        System.out.println("you are telling the truth");
    }

    return myRandomBoolean; // return the value at the end of the method
}

最后,您需要在 main 方法中调用此方法...您目前没有任何实际调用此方法的内容。

public static void main(String[] args){
    Scanner scanner = new Scanner(System.in);
    System.out.println("Type In a Question here");
    String q1 = scanner.nextLine();

    System.out.println("Insert Answer here");
    String a1 = scanner.nextLine();

    LieDetector lieDetector = new LieDetector();
    boolean truth = lieDetector.getRandomBoolean();
    // do stuff...
}

【讨论】:

    【解决方案2】:

    为您的方法提供内联 cmets

    public boolean getRandomBoolean() {
      // Create Random Generator:
      Random random = new Random();
      // generate a random boolean, stop running remainder, and return boolean
      return random.nextBoolean();
      // Whatever follows will never execute. javac and editor will
      // flag such dead code as an error.
      // If random value is true, then do nothing. Empty ';' = Do-Nothing
      if (random.boolean == true);
      // Always print "You are telling the truth"
      System.out.println("you are telling the truth");
    }
    

    现在我要大胆猜测您希望它如何运行:

    public boolean getRandomBoolean() {
      Random random = new Random();
      boolean randomBoolean = random.nextBoolean();
      if (randomBoolean) {
        System.out.println("you are telling the truth");
      }
      return randomBoolean;
    }
    

    【讨论】:

    • 不是DV,但不会编译
    • 啊——不错。看起来也只是稍早一点,答案相同。
    【解决方案3】:

    两个问题(getRandomBoolean

    1. return random.nextBoolean(); 将导致代码立即返回调用
    2. random.boolean 是无效语句,Random 没有名为 booleanboolean 字段,实际上是不可能的
    3. if (random.boolean == true); 末尾的; 会使语句短路(如果它可以编译),不管if 语句的结果如何,都会执行下一行代码。这就是为什么我们鼓励在 if 语句中使用 {...} 块,即使它们是单行,它实际上也更容易阅读(恕我直言)

    相反,让我们得到random.nextBoolean() 的结果,在if 语句中使用它,然后是return 它,例如...

    public boolean getRandomBoolean() {
        Random random = new Random();
        boolean value = random.nextBoolean();
        if (value) {
            System.out.println("you are telling the truth");
        }
        return value;
    }
    

    当我用您的代码替换它时,它会在我输入答案后终止。有什么想法吗?

    您需要实际调用方法 (getRandomBoolean) 并可能使用它的返回值,例如

    import java.util.Random;
    import java.util.Scanner;
    
    public class LieDetector {
    
        public static void main(String[] args) {
            LieDetector lieDetector = new LieDetector();
            lieDetector.runTest();
        }
    
        public void runTest() {
            Scanner scanner = new Scanner(System.in);
            System.out.println("Type In a Question here");
            String q1 = scanner.nextLine();
    
            System.out.println("Insert Answer here");
            String a1 = scanner.nextLine();
    
            if (getRandomBoolean()) {
                System.out.println("you are telling the truth");
            }
        }
    
        public boolean getRandomBoolean() {
            Random random = new Random();
            return random.nextBoolean();
        }
    }
    

    您可能想查看Classes and Objects 了解更多详情

    【讨论】:

    • 当我用你的代码替换它时(对不起,我只有 12 岁,仍然是菜鸟),它会在我输入答案后终止。有什么想法吗?
    • 你实际上并没有调用该方法
    • 所以我会说 System.out.println(value) 来调用它吗?对不起
    • 不,请检查更新,在调用 getRandomBoolean 方法之前,您需要一个 LieDetector 的实例,该方法返回一个 boolean 值,您可以使用它来做出决策跨度>
    【解决方案4】:

    绝对有可能。

    代码中的问题:

    您将if 语句放在错误的位置并在其后添加分号(;)。如果您将return 放在方法中的任何语句之前,则该语句将变为无法访问。如果你在if 后面加上一个分号,它将被忽略

    另外,Random 类下没有名为 boolean 的字段

    这可能是你想要的代码:

    import java.util.Random;
    import java.util.Scanner;
    
    public class LieDetector
    {
        public static void main(String[] args)
        {
            Scanner scanner = new Scanner(System.in);
            System.out.println("Type In a Question here");
            String q1 = scanner.nextLine();
    
            System.out.println("Insert Answer here");
            String a1 = scanner.nextLine();
    
            if (getRandomBoolean())
              System.out.println("You are telling the truth");
        }
    
        public static boolean getRandomBoolean()
        {
            Random random = new Random();
            return random.nextBoolean();
        }
    }
    

    顺便说一句,测试布尔值时不需要==booleanboolean == true 是一样的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-04
      • 2020-05-19
      • 1970-01-01
      • 2014-06-06
      • 2014-01-17
      • 1970-01-01
      • 2021-11-14
      • 1970-01-01
      相关资源
      最近更新 更多