我编写了一段代码来检查数组中的最大值,并在检查所有值后返回最大值。当我运行我的代码时,我不断收到错误。有什么意见吗?
// Initialize maximum to the first value
double maximum = measurements[0];
// Now go through the rest of the array
for (double i = 0; i < measurements.length; i++) {
if (i > maximum) {
maximum = i;
}
}
// At each step, compare the value to our current maximum
// If it's greater, replace maximum
System.out.println(maximum);
java.lang.AssertionError:预期结果不正确 [1019.1690418083019],但发现 [755.0]
最佳答案
请更改
if (i > maximum) {
maximum = i;
}
到
if (measurements[i] > maximum) {
maximum = measurements[i];
}
实际上,问题在于您将 i
与 maximum
进行比较,而不是 measurements[i]
关于java.lang.AssertionError : Incorrect result expected [1019. 1690418083019]但发现[755.0],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54396219/