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

时间:2015-06-21 06:00:58

标签: java if-statement 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");
    }
}

4 个答案:

答案 0 :(得分:0)

两个问题(getRandomBoolean

  1. return random.nextBoolean();会导致代码立即返回呼叫
  2. random.boolean是一个无效的陈述,Random没有名为boolean的{​​{1}}字段,实际上是不可能的
  3. boolean末尾的;会使语句短路(如果可以编译),执行下一行代码,而不管if (random.boolean == true);语句的结果如何。这就是为什么我们鼓励使用带有if语句的{...}块,即使它们是单行,它实际上也更容易阅读(恕我直言)
  4. 相反,让我们得到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

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

答案 1 :(得分:0)

  

为什么if语句行是错误的?

因为fragmentA.clear()未提供名为Random的字段。 (boolean ...)

  

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

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

random.boolean

此行将结束您的方法调用。在同一方法中,它下面的代码不会直接执行。

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

答案 2 :(得分:0)

为您的方法提供内联评论

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

答案 3 :(得分:0)

绝对可能。

代码中的问题:

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

此外,boolean

下没有名为Random的字段

这可能是您想要的代码:

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相同