繁体   English   中英

为什么 if 语句在随机布尔值之后不起作用?

[英]Why is the if statement not working after random boolean?

为什么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");
    }
}

两个问题(使用getRandomBoolean

  1. return random.nextBoolean(); 将导致代码立即返回调用
  2. random.boolean是无效语句, Random没有名为booleanboolean字段,实际上是不可能的
  3. ; if (random.boolean == true);的末尾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();
    }
}

您可能想查看 类和对象以获取更多详细信息

为什么 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...
}

为您的方法提供了内联注释

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;
}

这是绝对可能的。

您代码中的问题:

您将if语句放在错误的位置并在其后添加分号 ( ; )。 如果将return放在方法中的任何语句之前,该语句将变为unreachable 如果在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是一样的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM