简体   繁体   English

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

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

Why is the if statement line an error?为什么if语句行是错误的? im trying to get an if statement after a random boolean.我试图在一个随机布尔值之后得到一个if语句。 is this possible?这可能吗?

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

Two issues (with getRandomBoolean )两个问题(使用getRandomBoolean

  1. return random.nextBoolean(); will cause the code to return to the call immediately将导致代码立即返回调用
  2. random.boolean is an invalid statement, Random doesn't have a boolean field named boolean , it's actually impossible random.boolean是无效语句, Random没有名为booleanboolean字段,实际上是不可能的
  3. The ; ; at the end of if (random.boolean == true);if (random.boolean == true);的末尾if (random.boolean == true); would short circuit the statement (if it could compile), executing the very next line of code regardless of the result of the if statement.会使语句短路(如果它可以编译),无论if语句的结果如何,都会执行下一行代码。 This is why we encourage the use of {...} blocks with if statements, even if they are a single line, it's actually easier to read as well (IMHO)这就是为什么我们鼓励在if语句中使用{...}块,即使它们是一行,实际上也更容易阅读(恕我直言)

Instead, lets get the result of random.nextBoolean() , use that in the if statement, then return it, for example...相反,让我们获取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;
}

when i replace it with your code it terminates after i input the answer.当我用您的代码替换它时,它会在我输入答案后终止。 any ideas?有任何想法吗?

You need to actually call the method ( getRandomBoolean ) and maybe use it's return value, for example例如,您需要实际调用该方法( 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();
    }
}

You might like to have a look at Classes and Objects for more details您可能想查看 类和对象以获取更多详细信息

Why is the if statement line an error?为什么 if 语句行是错误的?

Because Random doesn't provide a field called boolean .因为Random不提供名为boolean的字段。 ( random.boolean ...) ( random.boolean ...)

im trying to get an if statement after a random boolean.我试图在一个随机布尔值之后得到一个 if 语句。 is this possible?这可能吗?

Yes, but you need proper syntax.是的,但您需要正确的语法。

return random.nextBoolean();

this line will end your method call.此行将结束您的方法调用。 No code directly below it in the same method will execute.在同一方法中,它下面的任何代码都不会执行。

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
}

Lastly, you need to call this method it in your main method... You don't currently have anything actually calling this 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...
}

Provided inline comments for your method为您的方法提供了内联注释

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

Now I am going to take a leap of faith in guessing how you wanted it to run:现在,我将大胆猜测您希望它如何运行:

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

It's absolutely possible.这是绝对可能的。

The problems in your code:您代码中的问题:

You put the if statement in a wrong place and add a semicolon( ; ) after it.您将if语句放在错误的位置并在其后添加分号 ( ; )。 If you put return before any statement in a method, the statement will become unreachable .如果将return放在方法中的任何语句之前,该语句将变为unreachable And if you put a semicolon after if , it will be ignored .如果在if后面加一个分号,它将被忽略

Also, there is no field called boolean under Random class此外,在Random类下没有名为boolean字段

This may be the code you want:这可能是您想要的代码:

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

BTW, you don't need == when you test a boolean.顺便说一句,测试布尔值时不需要== boolean and boolean == true are the same booleanboolean == true是一样的

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

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