gpt4 book ai didi

java - 如何比较数组中的值(整数)并将最小值存储为单独的变量?

转载 作者:太空宇宙 更新时间:2023-11-04 14:35:40 25 4
gpt4 key购买 nike

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];

当我应该得到 2.2 时,我最终会得到 3.3。对我如何做到这一点有什么建议吗?

最佳答案

if (priceArray[i] < priceArray[i - 1]) 

应替换为

if (priceArray[i] < lowestNum)

关于java - 如何比较数组中的值(整数)并将最小值存储为单独的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25612093/

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