gpt4 book ai didi

java - 将数组中的两个 double 相乘时收到 0.0

转载 作者:行者123 更新时间:2023-12-01 12:14:30 24 4
gpt4 key购买 nike

我在将数组中的两个 double 相乘时遇到了很大的麻烦。无论我做什么,结果似乎总是 0.0。我尝试过强制转换、将变量从 double 更改为 int 等。我做错了什么?很难找到重复的问题,但我非常确定存在一个。有人可以指导我吗?

我的代码:

double[] speedK = {100.0, 80.0, 90.0, 110.0, 100.0};
double[] speedMPH = new double[speedK.length];
int n=0;
for(double speedTemp : speedK)
{
speedMPH[n]= (double)speedTemp * 1.15078;
}
System.out.println(speedMPH[0]);

当我尝试打印 speedMPH 的任何值时,输出为 0.0

最佳答案

您仅修改 speedMPH 的第一个元素(n 始终为 0)。我建议使用普通的 for 循环,因为您需要当前索引。

for (int i = 0; i < speedK.length; i++) {
speedMPH[i] = speedK[i] * 1.15078;
}

无需转换为double。如果您确实想使用增强的 for 循环,则需要增加 n:

int n = 0;
for(double speedTemp : speedK) {
speedMPH[n] = speedTemp * 1.15078;
n++;
}

关于java - 将数组中的两个 double 相乘时收到 0.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27093659/

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