为什么这个方法不起作用?Java 三元运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16876698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why doesn't this method work? Java ternary operator
提问by David West
What's wrong with this code:
这段代码有什么问题:
void bark(boolean hamlet) {
hamlet ? System.out.println("To Bark.") : System.out.println("Not to Bark");
}
回答by greedybuddha
Ternary operators can't have statements that don't return values, void
methods. You need statements that have return values.
三元运算符不能有不返回值、void
方法的语句。您需要具有返回值的语句。
You need to rewrite it.
你需要重写它。
void bark(boolean hamlet) {
System.out.println( hamlet ? "To Bark." : "Not to Bark" );
}
回答by Keppil
You can read why in the Java Language Specification, 15.25. Conditional Operator ? :
您可以在 Java 语言规范15.25 中了解原因。条件运算符 ? :
It is a compile-time error for either the second or the third operand expression to be an invocation of a void method.
第二个或第三个操作数表达式调用 void 方法是编译时错误。
You need to do as several of the other answers suggest, and apply the conditional operator to just the argument.
您需要按照其他几个答案的建议进行操作,并将条件运算符应用于参数。
回答by Eng.Fouad
According to §JLS.15.25:
根据§JLS.15.25:
ConditionalExpression: ConditionalOrExpression ConditionalOrExpression ? Expression : ConditionalExpression
ConditionalExpression: ConditionalOrExpression ConditionalOrExpression ? Expression : ConditionalExpression
The conditional operator is syntactically right-associative (it groups right-to-left). Thus, a?b:c?d:e?f:g means the same as a?b:(c?d:(e?f:g)).
The conditional operator has three operand expressions. ? appears between the first and second expressions, and : appears between the second and third expressions.
The first expression must be of type boolean or Boolean, or a compile-time error occurs.
It is a compile-time error for either the second or the third operand expression to be an invocation of a void method.
条件运算符在语法上是右结合的(它从右到左分组)。因此,a?b:c?d:e?f:g 的含义与 a?b:(c?d:(e?f:g)) 相同。
条件运算符具有三个操作数表达式。? 出现在第一个和第二个表达式之间,并且 : 出现在第二个和第三个表达式之间。
第一个表达式必须是 boolean 或 Boolean 类型,否则会发生编译时错误。
第二个或第三个操作数表达式调用 void 方法是编译时错误。
回答by Paul Sullivan
I should imagine its because the ternary operator is expecting to assign a value. Try this:
我应该想象它是因为三元运算符期望分配一个值。试试这个:
void bark(boolean hamlet) {
String result = hamlet ? "To Bark!" : "Not to Bark";
System.out.println(result)
}
回答by Steve P.
Ternary operators must return something. So you can put it inside the print statement like such:
三元运算符必须返回一些东西。所以你可以像这样把它放在打印语句中:
void bark(boolean hamlet)
{
System.out.printf("%s\n", hamlet ? "To Bark." : "Not to Bark");
}
Or:
或者:
void bark(boolean hamlet)
{
System.out.println(hamlet ? "To Bark." : "Not to Bark");
}
回答by Djon
A ternary statement has to return something, you could use an if here:
三元语句必须返回一些东西,你可以在这里使用 if:
void bark(boolean hamlet)
{
if (hamlet)
{
System.out.println("To Bark.")
}
else
{
System.out.println("Not to Bark");
}
}