java 在单行中分配和执行 if/else 条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28236057/
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
assign and execute if/else conditions in single line
提问by peti446
How can I do that for example, if
block checks if the condition is true
or false
and then execute action depends on that condition?
我该怎么做,例如,if
块检查条件是否为true
或false
,然后根据该条件执行操作?
like,
喜欢,
if (check true or false)
if false do action
if true do action
Something like that do it all in the same line without blocks, it that possible ?
这样的事情在没有块的情况下在同一行中完成,这可能吗?
Sorry for my bad English
对不起,我的英语不好
Thanks.
谢谢。
采纳答案by Sufiyan Ghori
You can use java ternary operator,
您可以使用java 三元运算符,
result = testCondition ? value1 : value2
this statement can be read as, If testCondition
is true, assign the value of value1
to result
; otherwise, assign the value of value2
to result
.
这条语句可以理解为,如果testCondition
为真,则赋值value1
给result
;否则,分配的值value2
来result
。
simple example would be,
简单的例子是,
minVal = a < b ? a : b;
In above code, if the variable a
is less than b
, minVal
is assigned the value of a
; otherwise, minVal
is assigned the value of b
.
在上面的代码中,如果变量a
小于b
,minVal
则赋值为a
; 否则,minVal
被赋值为b
。
here is another example using String
,
这是使用的另一个示例String
,
// result is assigned the value "it's false"
String result = false ? "that was true" : "it's false";
this examples would clear it more. It prints the salutation properly for a person's gender:
这个例子会更清楚。它为一个人的性别正确打印称呼:
// if `person.isMale()` then its Mr. else its Ms.
return = "Thank you " + (person.isMale() ? "Mr. " : "Ms. ") + person.getLastName() + ".";
edit:
编辑:
Question: you cannot call a method inside ternary operator.
问题:您不能在三元运算符中调用方法。
No, this is incorrect.
不,这是不正确的。
You can call a method inside a ternary operator as long as it returns a value which is of the same type as the left-side of a ternary operator,
你可以在三元运算符内部调用一个方法,只要它返回一个与三元运算符左侧相同类型的值,
consider this example,
考虑这个例子,
let's create a method that returns a String
value,
让我们创建一个返回String
值的方法,
String message(){
return "This is returned value";
}
Now see this code,
现在看到这个代码,
String answer = false ? "true" : message();
this will execute just fine because return
type of message()
and variable type of answer
are the same, i.e String
.
这将执行得很好,因为return
类型message()
和变量类型answer
是相同的,即String
。
output:
输出:
This is returned value
回答by tobias_k
For putting a conditional statement on one line, you coulduse the ternary operator. Note, however, that this can only be used in assignments, i.e. if the functions actually return something and you want to assign that value to a variable.
要将条件语句放在一行上,您可以使用三元运算符。但是请注意,这只能用于赋值,即如果函数实际上返回了一些东西并且您想将该值分配给一个变量。
int abs = x > 0 ? x : -1;
If you want to execute methods, that do not return anything, but have some other side-effects, the ternary operator is not the right way to go. In fact, it does not even compile!
如果要执行不返回任何内容但具有其他副作用的方法,则三元运算符不是正确的方法。事实上,它甚至不编译!
void doThis() {/* do this */}
void doThat() {/* do that */}
(condition) ? doThis() : doThat(); // does not work!
Instead, you should just use a plain-old if-then-else
. It's also easier to read.
相反,您应该只使用普通的if-then-else
. 它也更容易阅读。
if (condition) {
doThis();
} else {
doThat();
}
回答by Dave
max = (a > b) ? a : b;
Is logically identical to
在逻辑上等同于
if (a > b) {
max = a;
} else { max = b; }
回答by Neeraj Jain
You can not use Ternary Operator as you can not invoke any void method(i.e. the method which do not return anything ) from Ternary Operator
您不能使用三元运算符,因为您不能从中调用任何void 方法(即不返回任何内容的方法)Ternary Operator
As it is not the intended use of the ternary operator.
因为它不是三元运算符的预期用途。
If you really want it to be achieved in 1 line, you can write:
如果你真的希望它在 1 行中实现,你可以写:
if (condition) doThisMethod(); else doThatMethod();
回答by Cristiano Fontes
If you really, really need a one-liner you could use this syntax (Known as ternary operator) :
如果你真的,真的需要一个单行你可以使用这个语法(称为三元运算符):
1>0 ? "do action" : " Do other action";
1>0 ? "do action": "做其他动作";
An example would be this:
一个例子是这样的:
String result = x>y ? "x>y" : "y>x";
字符串结果 = x>y ? "x>y" : "y>x";
But this is not recommended, most people hate this syntax because it can become very unreadable if you cascade several conditions inside it, or even worst cascade ternary operators.
但这不是推荐的,大多数人讨厌这种语法,因为如果在其中级联多个条件,甚至最糟糕的级联三元运算符,它会变得非常不可读。
But it has it's uses.
但它有它的用途。
回答by André Laszlo
Maybe the ternary operatordoes what you want:
也许三元运算符可以满足您的需求:
checkIfTrue() ? actionIfTrue() : actionIfFalse();
回答by Eypros
You can use a trenary operator:
您可以使用三元运算符:
if(condition) ? executeForTrue() : executeForFalse();
which I guess pretty much does what you want.
我想这几乎可以满足您的需求。