我想要求更多澄清。这是我的示例程序
double diff = 7.500 - 7.500;
System.out.println(diff); // result 0.0
if (diff > 0) {
System.out.println("+" + diff ); //result +0.0
} else {
System.out.println("-" + diff ); //result -0.0
}
我的结果是 -0.0 。我的期望是0 == 0.0然后跳过if else条件。但是它进入了else条件。双倍0.0大于或小于0?
答案 0 :(得分:9)
请注意,您正在检查diff> 0 - 所以如果它为零,则表示正在打印-0.0
答案 1 :(得分:4)
在您的情况下,您使用(更大)>
将0.0与0进行比较,因此0.0不是严格大于0,(它是相等的)。然后您的程序转到else
部分。
if(diff == 0 ) {
System.out.println("+" + diff ); //result 0.0
} else if (diff > 0) {
System.out.println("+" + diff ); //result +diff
} else { // diff is less than 0
System.out.println("-" + diff ); //result -diff
}
答案 2 :(得分:2)
这是我的答案:
double diff = 7.500 - 7.500;
System.out.println(diff);
if(diff>0){
System.out.println("+"+diff);
}else if(diff<0){
System.out.println("-"+diff);
}else if(diff==0){
System.out.println("="+diff);
}
}
我的结果是:
0.0
= 0.0
你必须忘记0.0等于0是'否则0.0&gt; 0'其他'0.0&gt; = 0'