如何找到数组 java 中的最大值和最小值?
How to find the max and min value in an array java?
有人能告诉我为什么我不能得到这些物品的价格和数量相乘的最高价值和最低价值吗?
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: .45
Lowest: Item: apple, Inventory Value: .80000000000001
Lowest: Item: grapes, Inventory Value: 5.0
HIGHEST: Item: potato, Inventory Value: .25
由于您没有提供预期的输出,我将只说明逻辑错误和我的快速修复。
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: 5.0
Lowest:
Item: potato, Inventory Value: .25
有人能告诉我为什么我不能得到这些物品的价格和数量相乘的最高价值和最低价值吗?
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: .45
Lowest: Item: apple, Inventory Value: .80000000000001
Lowest: Item: grapes, Inventory Value: 5.0
HIGHEST: Item: potato, Inventory Value: .25
由于您没有提供预期的输出,我将只说明逻辑错误和我的快速修复。
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: 5.0
Lowest:
Item: potato, Inventory Value: .25