我得到了这个简单的JavaScript赋值语句的意外结果:
var t = 1 == 1 ? 1 : 0;
undefined
Run Code Online (Sandbox Code Playgroud)
我原本希望将1分配给v代替.如果你这样做,结果相同
var t = (1 == 1 ? 1 : 0);
undefined
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么这不能按预期工作吗?
有没有办法在没有做任务的情况下进行java三元操作或假冒伪造?
我喜欢在做一堆if/then/elses时简洁的三元代码看起来如何.
我希望能够基于布尔代数语句调用两个void函数之一.
就像是:
(bool1 && bool2) ? voidFunc1() : voidFunc2();
我的函数是返回类型void,所以如果有一种方法可以在赋值中伪造它以使其工作,那么我对此没问题......我想看看如何做到这一点:)
只花了几个小时调试这段代码:
LinkedHashMap<String, Integer> rsrqs = new LinkedHashMap<String, Integer>();
Integer boxedPci = 52;
Integer boxedRsrq = boxedPci != null ? rsrqs.get(boxedPci.toString()) : -1;
Run Code Online (Sandbox Code Playgroud)
以上产生了NullPointerException.以下代码不:
LinkedHashMap<String, Integer> rsrqs = new LinkedHashMap<String, Integer>();
Integer boxedPci = 52;
Integer boxedRsrq = boxedPci != null ? rsrqs.get(boxedPci.toString()) : Integer.valueOf(-1);
Run Code Online (Sandbox Code Playgroud)
唯一的区别是用Integer.valueOf()包装-1.一旦有人解释了为什么这个代码的行为方式,我肯定我会打破我的前额..但有人可以向我解释为什么这个代码的行为方式:)
- 编辑
第二个想法,我怀疑NPE来自rsrqs.get()返回null,我认为java在尝试解包成int之前,然后再回到整数.Integer.valueOf()强制Java执行unbox-box步骤.故事的道德启示; 不要只忽略Eclipse中的那些拳击警告;)
我有以下代码:
class A {
public:
operator int() const { return 5; }
};
class B {
public:
operator int() const { return 6; }
};
int main() {
A a;
B b;
int myInt = true ? a : b;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
尝试使用Visual Studio 2017 RC编译该代码会导致以下错误:
错误C2446 ::
:没有转换B为A注意:没有可用于执行此转换的用户定义转换运算符,或者无法调用运算符
...这是令人惊讶的,因为在这种情况下,我希望它将它们转换为通用类型int.
clang (4.0)成功编译相同的代码,没有任何错误或警告.
在这种情况下,哪两个是正确的,为什么?
c++ type-conversion ternary implicit-conversion operator-keyword
在三元运算符中调用的方法递增变量并返回布尔值.当函数返回false时,将还原该值.我期望变量为1但是变为0.为什么?
public class Main {
public int a=0;//variable whose value is to be increased in function
boolean function(){
a++;
return false;
}
public static void main(String argv[]){
Main m=new Main();
m.a+=(m.function()?1:0);
System.out.println(m.a);//expected output to be 1 but got a 0 !!!!!
}
}
Run Code Online (Sandbox Code Playgroud) 首先,问题是"编写一个Java程序,使用三元运算符找到三个最小的数字."
这是我的代码:
class questionNine
{
public static void main(String args[])
{
int x = 1, y = 2, z = 3;
int smallestNum;
smallestNum = (x<y && x<z) ? x : (y<x && y<z) ? y : (z<y && z<x) ? z;
System.out.println(smallestNum + " is the smallest of the three numbers.");
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试在三元运算符中使用多个条件,但这不起作用.几天我不在,所以我不确定该做什么,老师的电话已关闭.有帮助吗?
我想用?决定我想要调用哪种方法,但我不需要分配变量.我的问题:有没有办法使用三元运算符而不分配变量?
(something i dont need) = (x == 1)? doThisMethod():doThatMethod()
Run Code Online (Sandbox Code Playgroud)
代替
if(x == 1) {
doThisMethod()
} else {
doThatMethod()
}
Run Code Online (Sandbox Code Playgroud) 前NullPointerException几天我在三元运算符中意外地进行了类型转换,这让我感到非常奇怪.鉴于此(无用的示例)功能:
Integer getNumber() {
return null;
}
Run Code Online (Sandbox Code Playgroud)
我希望编译后以下两个代码段完全相同:
Integer number;
if (condition) {
number = getNumber();
} else {
number = 0;
}
Run Code Online (Sandbox Code Playgroud)
与
Integer number = (condition) ? getNumber() : 0;
Run Code Online (Sandbox Code Playgroud)
.
事实证明,如果condition是true,if-statement工作正常,而第二个代码段中的三元操作抛出一个NullPointerException.似乎三元操作决定int在将结果自动装箱之前将两种选择都输入到一个Integer!?!事实上,如果我明确地转换0为Integer,则异常消失.换一种说法:
Integer number = (condition) ? getNumber() : 0;
Run Code Online (Sandbox Code Playgroud)
是不一样的:
Integer number = (condition) ? getNumber() : (Integer) 0;
Run Code Online (Sandbox Code Playgroud)
.
因此,似乎三元运算符和等效if-else语句之间存在字节码差异(我没想到的事情).这提出了三个问题:为什么会有差异?这是三元实现中的错误还是有类型转换的原因?鉴于存在差异,三元运算的性能是否与等效的if陈述相比或多或少(我知道,差异不是很大,但仍然存在)?
如果我没有弄错的话,PEP8对三元运算符没有任何说明.那么你有什么建议,我应该如何用三元条件运算符编写长行?
some_variable = some_very_long_value \
if very_long_condition_holds \
else very_long_condition_doesnt_hold
Run Code Online (Sandbox Code Playgroud)
要么
some_variable = some_very_long_value \
if very_long_condition_holds \
else very_long_condition_doesnt_hold
Run Code Online (Sandbox Code Playgroud)
你最喜欢哪一个?
ternary ×10
java ×6
autoboxing ×2
c ×1
c++ ×1
formatting ×1
if-statement ×1
increment ×1
javascript ×1
pep8 ×1
python ×1