gpt4 book ai didi

java - 双变量实现和不同的输出

转载 作者:行者123 更新时间:2023-11-29 04:46:56 24 4
gpt4 key购买 nike

我想知道为什么有 2 个不同的输出:

double a = 88.0;
System.out.println(a + 10); // 98.0
double result = 88.0;
System.out.println("The result is " + result + 10); // The result is 88.010

最佳答案

当你计算 "the result is "+ result + 10您正在评估 String + double + int

执行此操作时,首先将 double 添加到字符串中,创建另一个字符串,然后将 int 添加到该字符串中,生成另一个字符串。

所以你得到:

"the result is " + result + 10
"the result is 88.0" + 10
"the result is 88.010"

这不同于

"the result is " + (result+10)

这会给

"the result is 98.0"

关于java - 双变量实现和不同的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36767252/

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