如何在数组java中找到最大值和最小值?
Posted
技术标签:
【中文标题】如何在数组java中找到最大值和最小值?【英文标题】:How to find the max and min value in an array java? 【发布时间】:2021-02-10 11:48:34 【问题描述】:谁能告诉我为什么我不能得到这些物品的价格和数量相乘的最高价值和最低价值?
public class StoreProgram
public static void main(String[] args)
String[] storeItems =
"broccoli", "onion", "carrot", "turnip", "mango",
"bread", "garlic", "celery", "apple", "banana",
"raisins", "grapes", "lemon", "orange", "potato";
int[] itemQuantities =
23, 5, 7, 15, 2,
13, 13, 8, 20, 30,
3, 25, 10, 9, 1;
double[] itemPrices =
2.0, 0.89, 0.70, 1.50, 2.99,
3.45, 1.45, 1.12, 3.99, 0.25,
4.99, 7.00, 1.75, 1.80, 3.25;
double max = itemQuantities[0] * itemPrices[0];
double min = itemQuantities[0] * itemPrices[0];
for (int i = 1; i < storeItems.length; i++)
if (max > itemQuantities[i] * itemPrices[i])
max = itemQuantities[i] * itemPrices[i];
System.out.println("HIGHEST:\n\tItem: " + storeItems[i]
+ ",\t" + "Inventory Value: $" + max);
if (min < itemQuantities[i] * itemPrices[i])
min = itemQuantities[i] * itemPrices[i];
System.out.println("Lowest:\n\tItem: " + storeItems[i]
+ ",\t" + "Inventory Value: $" + min);
它打印出以下内容:
HIGHEST: Item: onion, Inventory Value: $4.45
Lowest: Item: apple, Inventory Value: $79.80000000000001
Lowest: Item: grapes, Inventory Value: $175.0
HIGHEST: Item: potato, Inventory Value: $3.25
【问题讨论】:
我能看到的唯一错误是 (1)<
和 >
被颠倒了,(2) 你可能有一个浮点算术错误,(3) 你正在显示在循环内输出,而不是在循环结束时输出。这将使您的最大值显示为最小值,反之亦然,并且还可能显示带有太多小数位的金额。它还会重复显示输出,而不仅仅是一次。所有这些都是正在发生的事情吗?
@DawoodibnKareem 不,我希望它是十进制哈哈。它打印出以下内容:最高:项目:洋葱,库存价值:4.45 美元最低:项目:苹果,库存价值:79.80000000000001 最低:项目:葡萄,库存价值:175.0 美元最高:项目:土豆,库存价值:3.25 美元
听起来像我描述的那样。所以你有三个单独的错误,要单独修复。修复 <
和 >
标志。将输出线移到循环外。然后阅读如何使用BigDecimal
类。
@DawoodibnKareem,当我将打印语句移到循环之外时,storeItems[i] 在循环中声明之前不起作用
Chass 非常友好地为您修复了三个错误中的两个!
【参考方案1】:
由于您没有提供预期的输出,我将仅说明逻辑错误和我的快速修复。
if (max > itemQuantities[i] * itemPrices[i])
max = itemQuantities[i] * itemPrices[i];
System.out.println("HIGHEST:\n\tItem: " + storeItems[i]
+ ",\t" + "Inventory Value: $" + max);
是错误的,因为您正在比较可能的最大值,然后如果最大值更高,则将其分配给最大值,这没有意义,同样的逻辑错误会达到最小值。
其次,每次更改时都会打印出值。由于您要求的是最高值和最低值,因此每个值应该只有一个。
这就是我的工作
public class StoreProgram
public static void main(String[] args)
String[] storeItems =
"broccoli", "onion", "carrot", "turnip", "mango",
"bread", "garlic", "celery", "apple", "banana",
"raisins", "grapes", "lemon", "orange", "potato";
int[] itemQuantities =
23, 5, 7, 15, 2,
13, 13, 8, 20, 30,
3, 25, 10, 9, 1;
double[] itemPrices =
2.0, 0.89, 0.70, 1.50, 2.99,
3.45, 1.45, 1.12, 3.99, 0.25,
4.99, 7.00, 1.75, 1.80, 3.25;
double max = itemQuantities[0] * itemPrices[0];
double min = itemQuantities[0] * itemPrices[0];
int highindex, lowindex;
highindex = lowindex = 0;
for (int i = 1; i < storeItems.length; i++)
if (max < itemQuantities[i] * itemPrices[i])
max = itemQuantities[i] * itemPrices[i];
highindex = i;
if (min > itemQuantities[i] * itemPrices[i])
min = itemQuantities[i] * itemPrices[i];
lowindex = i;
System.out.println("HIGHEST:\n\tItem: " + storeItems[highindex]
+ ",\t" + "Inventory Value: $" + max);
System.out.println("Lowest:\n\tItem: " + storeItems[lowindex]
+ ",\t" + "Inventory Value: $" + min);
输出如下
HIGHEST:
Item: grapes, Inventory Value: $175.0
Lowest:
Item: potato, Inventory Value: $3.25
【讨论】:
以上是关于如何在数组java中找到最大值和最小值?的主要内容,如果未能解决你的问题,请参考以下文章