可能重复:
什么是Java?:运算符被调用,它做了什么?
我试图阅读二叉树的实现,我遇到了这一行代码:
if (...) {
...
} else {
node = ( node.left != null ) ? node.left : node.right; //this line
}
return node;
Run Code Online (Sandbox Code Playgroud)
谁能告诉我这条线的含义是什么?我最好的猜测是,这是某种条件陈述.
我知道三元运算符用于在评估逻辑条件后进行变量赋值.
所以:
String s = (logicalVariable) ? "Hello" : "Bye, bye";
Run Code Online (Sandbox Code Playgroud)
要么,
int x = (5<3) ? 10 : 100;
Run Code Online (Sandbox Code Playgroud)
将是用法示例,对吧?
但是,如果我真的不想做变量赋值但是设置一个句子甚至一段代码,例如:
if (inventario.containsKey(item))
return inventario.get(item);
else return Integer.MIN_VALUE;
Run Code Online (Sandbox Code Playgroud)
可以使用三元运算符将这些行写成一行表达式吗?
对于三元运算符来说,我是一个初学者,以前从未与他们合作过.
代码(简化)
$output2 = '
<div>
<div>
<span>test text1</span>
<div>
'.(1 == 1) ? "yes" : "no" .'
<span>test text 2</span>
</div>
</div>
</div>';
echo $output2;
Run Code Online (Sandbox Code Playgroud)
所以问题是,这段代码只输出"是"(只有正确或错误的if语句)
我尝试了""同样的问题,尝试了不同的条件,尝试输出它,没有变量.但问题仍然存在.
谢谢.
Sebastjan
I'm trying to clean up some code with ternary operators and I'm running into some compiler errors i can't make sense of.
The code I had before looks like this and runs fine.
if(!inFile.good())
throw -2;
getline(inFile, inLine);
Run Code Online (Sandbox Code Playgroud)
And I'm trying to clean it up using this code instead.
(inFile.good()) ? getline(inFile, inLine) : throw -2;
Run Code Online (Sandbox Code Playgroud)
But I'm getting the following errors.
g++ -w -o test orange_test.cpp
In file included from orange_test.cpp:4:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/iostream:39:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/streambuf:558:31: error: base …Run Code Online (Sandbox Code Playgroud) 我想做一些非常简单的事情:
if (getStatus()) {
require_once('a.php');
} else {
require_once('b.php');
}
function getStatus() {
return true; //For the sake of this demo
}
Run Code Online (Sandbox Code Playgroud)
如何使用三元运算符重写?
我假设这样的事情:
getStatus() ? require_once('a.php') : require_once('b.php');
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚语法.我见过的所有例子都用它来做某事.例如x = y ? a : b;,但我不想以这种方式使用它.
我想做什么甚至可能吗?
更复杂:
这也可以用三元形式表达吗?
if (websiteIsLive()) {
setcookie("token", x, 0, '/', $_SERVER['HTTP_HOST'], true);
} else {
setcookie("token", y, 0, '/');
}
Run Code Online (Sandbox Code Playgroud) 什么意思是" 返回p?memcpy(p,s,len):NULL; "在下面的代码中?(更一般地说,条件运算符是什么,a ? b : c?)
char * strdup(const char * s)
{
size_t len = 1+strlen(s);
char *p = malloc(len);
return p ? memcpy(p, s, len) : NULL;
}
Run Code Online (Sandbox Code Playgroud) 我有条件:
if (dr_dados["DAT_SAIDA"] != null)
{
txtDataSaida.Text = "";
}
else
{
txtDataSaida.Text = dr_dados["DAT_SAIDA"].ToString();
}
Run Code Online (Sandbox Code Playgroud)
我正在使用Jetbrain的ReSharper,它告诉我可以转换成三元操作.
所以,它变成了这样:
txtDataSaida.Text = (dr_dados["DAT_SAIDA"] != null) ? dr_dados["DAT_SAIDA"].ToString() : "";
Run Code Online (Sandbox Code Playgroud)
但后来它告诉我,我可以转换为空合并操作,它给了我这个:
txtDataSaida.Text = dr_dados["DAT_SAIDA"]?.ToString() ?? "";
Run Code Online (Sandbox Code Playgroud)
我有点知道null合并操作的作用,但是有一些不同的东西,我以前没见过的东西,我想知道它是什么.
这个额外的审讯就在这里:
v
txtDataSaida.Text = dr_dados["DAT_SAIDA"]?.ToString() ?? "";
Run Code Online (Sandbox Code Playgroud)
它是什么意思/是什么意思?
c# resharper ternary-operator null-coalescing-operator c#-6.0
我试图在输入为空时设置背景,并在有一些输入时保持此值
function emptyto(element){
return element.value == '' ? element.style.backgroundColor = "#ccc" : element.val()
}
Run Code Online (Sandbox Code Playgroud) 因此,在查看大规模的互联网后,我无法找到答案.
说我有这段代码:
if(P4IN & GPIO_PIN1?0:1){
if (state==1){
state = 0;
//Wait for this state to pass -- ends up saving the current state on button press.
while (counter < 10000){
counter++;
}
}
else{
state = 1;
while (counter < 10000){
counter++;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我怎么会重写这个,所以if(P4IN & GPIO_PIN1?0:1)不是这样写的.我不介意创建额外的if/else条件或扩展此代码块(用于MSP432)
谢谢你的时间.
condition ?
domElement.classList.add('show') :
domElement.classList.remove('show');
Run Code Online (Sandbox Code Playgroud)
上面的代码可以工作,但DOM变量和classList被显式输入两次.有没有办法使用三元组只将链中的差异部分放在各自的真/假条款中?
我想的是:
domElement.classList condition ? .add('show') : .remove('show');
Run Code Online (Sandbox Code Playgroud)
任何和所有输入都非常感谢.
javascript code-duplication ternary-operator method-chaining
ternary-operator ×10
c ×2
if-statement ×2
java ×2
javascript ×2
php ×2
binary-tree ×1
c# ×1
c#-6.0 ×1
c++ ×1
function ×1
operators ×1
resharper ×1
std ×1