gpt4 book ai didi

java - 读取和解释程序中的逻辑表达式

转载 作者:行者123 更新时间:2023-11-30 06:41:52 25 4
gpt4 key购买 nike

您通常如何阅读程序中的逻辑表达式?例如:

(1 == x) && ( 2 > x++)? (x=1)

的目的是什么?表达式正确答案的生成思路是什么?

最佳答案

下面的语句:

var value = (boolean expression) ? some value if `true` : some value if `false`

是一个特殊的条件语句,它使用了 Ternary Operator (?:) 根据 boolean 表达式为变量赋值。

这是表达此条件语句的更简洁的方式:

var value;

//this is the boolean expression you evaluate before the question mark
if (boolean expression is true) {
//this is what you assign after the question mark
value = some value if true;
}
else {
//this is what you assign after the colon
value = some other value if false;
}

因此,根据您的示例(顺便说一句,语法错误),这将类似于:

if ((1 == x) && (2 > x++)){
x = 1;
}
else {
/*This is the value that would be put after the colon
*(which is missing in your example, and would cause a compiler error)
*/
x = some other value;
}

这将转化为:

x = (1 == x) && (2 > x++) ? 1 : some other value

关于java - 读取和解释程序中的逻辑表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54531092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com