为什么 Java 不允许在这里使用三元运算符?

Why doesn't Java allow the use of a ternary operator here?

而不是键入:

if (Math.random() < .5) {
    System.out.println("toto");
} else {
    System.out.println("tata");
}

我会发现键入以下内容很有用且合乎逻辑:

Math.random() < .5 ? System.out.println("toto") : System.out.println("tata");

但是,我收到错误 not a statement。我不明白这是怎么回事。

因为三元运算符给变量赋值。将其更改为:

String toPrint = Math.random() < .5 ? "toto" : "tata";
System.out.println(toPrint);