简体   繁体   中英

Why is the if statement not working after random boolean?

Why is the if statement line an error? im trying to get an if statement after a random boolean. 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 )

  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
  3. The ; at the end of 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. 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)

Instead, lets get the result of random.nextBoolean() , use that in the if statement, then return it, for example...

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

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?

Because Random doesn't provide a field called boolean . ( random.boolean ...)

im trying to get an if statement after a random boolean. 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.

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 you put return before any statement in a method, the statement will become unreachable . And if you put a semicolon after if , it will be ignored .

Also, there is no field called boolean under Random class

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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