Can someone tell me why I can't get the highest value and minimum of value of multiplying both the price and the quantity of these items?
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);
}
}
}
}
It prints out the following:
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
<and>are reversed, (2) you've potentially got a floating point arithmetic error, (3) you're displaying output within the loop, instead of at the end of it. That would make your maximum show as the minimum and vice versa, and also possibly show an amount with too many decimal places. It would also show output repeatedly, instead of just once. Are all those things what's happening?<and>signs. Move the output lines to outside the loop. Then read up on how to use theBigDecimalclass.