Java 简短的 IF - ELSE 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4461996/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 17:49:58  来源:igfitidea点击:

Short IF - ELSE statement

javaif-statement

提问by monczek

I'm trying to make my code more readable, so I decided to use some short IF statements.

我试图让我的代码更具可读性,所以我决定使用一些简短的 IF 语句。

Here's my code which doesn't work ("not a statement"):

这是我的代码不起作用(“不是声明”):

jXPanel6.isVisible() ? jXPanel6.setVisible(true) : jXPanel6.setVisible(false);

What's wrong with this? Needs brackets? Where?

这有什么问题?需要括号吗?在哪里?

采纳答案by Andrzej Doyle

The "ternary expression" x ? y : zcan only be used for conditional assignment. That is, you could do something like:

“三元表达式”x ? y : z只能用于条件赋值。也就是说,您可以执行以下操作:

String mood = inProfit() ? "happy" : "sad";

because the ternary expression is returning something (of type Stringin this example).

因为三元表达式正在返回一些东西(String在这个例子中是类型)。

It's not really meant to be used as a short, in-line if-else. In particular, you can't use it if the individual parts don't return a value, or return values of incompatible types. (So while you could do this if both method happened to return the same value, you shouldn'tinvoke it for the side-effect purposes only).

它并不是真的要用作简短的内联if-else. 特别是,如果各个部分不返回值或返回不兼容类型的值,则不能使用它。(因此,如果两种方法碰巧返回相同的值,您可以执行此操作,但不应仅出于副作用目的调用它)。

So the proper way to do this would just be with an if-else block:

因此,正确的方法是使用 if-else 块:

if (jXPanel6.isVisible()) {
    jXPanel6.setVisible(true);
}
else {
    jXPanel6.setVisible(false);
}

which of course can be shortened to

这当然可以缩短为

jXPanel6.setVisible(jXPanel6.isVisible());

Both of those latter expressions are, for me, more readable in that they more clearly communicate what it is you're trying to do. (And by the way, did you get your conditions the wrong way round? It looks like this is a no-op anyway, rather than a toggle).

对我来说,后两种表达方式更具可读性,因为它们更清楚地传达了您正在尝试做什么。(顺便说一句,你的条件是不是弄错了?无论如何,这看起来是一个空操作,而不是一个切换)。

Don't mix up low character countwith readability. The key point is what is most easily understood; and mildly misusing language features is a definite way to confuse readers, or at least make them do a mental double-take.

不要将低字符数可读性混为一谈。关键是最容易理解的;轻微滥用语言特征肯定会迷惑读者,或者至少让他们在心理上做两件事。

回答by mauretto

jXPanel6.setVisible(jXPanel6.isVisible());

or in your form:

或以您的形式:

jXPanel6.setVisible(jXPanel6.isVisible()?true:false);

回答by perdian

The ternary operator can only be the right side of an assignment and not a statement of its own.

三元运算符只能是赋值的右侧,而不是它自己的语句。

http://www.devdaily.com/java/edu/pj/pj010018/

http://www.devdaily.com/java/edu/pj/pj010018/

回答by Anshuman Fotedar

As others have indicated, something of the form

正如其他人所指出的,某种形式

x ? y : z

is an expression, not a (complete) statement. It is an rvalue which needs to get used someplace - like on the right side of an assignment, or a parameter to a function etc.

是一个表达式,而不是(完整的)语句。它是一个需要在某个地方使用的右值 - 例如在赋值的右侧,或函数的参数等。

Perhaps you could look at this: http://download.oracle.com/javase/tutorial/java/nutsandbolts/expressions.html

也许你可以看看这个:http: //download.oracle.com/javase/tutorial/java/nutsandbolts/expressions.html

回答by Andy Body

I'm a little late to the party but for future readers.

我参加聚会有点晚了,但对于未来的读者来说。

From what i can tell, you're just wanting to toggle the visibility state right? Why not just use the !operator?

据我所知,您只是想切换可见性状态,对吗?为什么不直接使用!运算符?

jxPanel6.setVisible(!jxPanel6.isVisible);

It's not an if statement but I prefer this method for code related to your example.

这不是 if 语句,但我更喜欢这种与您的示例相关的代码的方法。

回答by Steven S.

You can do it as simple as this, I did it in react hooks :

你可以像这样简单地做到这一点,我在 react hooks 中做到了:

 (myNumber == 12) ? "true" : "false"

it was equal to this long if function below :

它等于下面这个长的 if 函数:

if (myNumber == 12) {
  "true"
} else {
  "false"
}

Hope it helps ^_^

希望对你有帮助^_^