double lowestNum = priceArray[0]; //setting a default value just in case none of the
//cases passes the if statement
for (int i = 1; i < priceArray.length; i++) {
if (priceArray[i] < priceArray[i - 1]) {
lowestNum = priceArray[i]; //what I'm trying to do is rewrite this
//value to the "leastCost" variable
}
}
System.out.println("The lowest number is: " + lowestNum);
}
在这段代码中,我最终将获得一个0,priceArray [3] = {2.2,2.4,3.3}。如果我更改:
lowestNum = priceArray[i];
至
lowestNum += priceArray[i];
我最终将获得3.3,而我应该获得2.2。关于如何执行此操作的任何建议?
最佳答案
if (priceArray[i] < priceArray[i - 1])
应该替换为
if (priceArray[i] < lowestNum)