簡體   English   中英

如何在數組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: $4.45
Lowest: Item: apple, Inventory Value: $79.80000000000001
Lowest: Item: grapes, Inventory Value: $175.0
HIGHEST: Item: potato, Inventory Value: $3.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: $175.0
Lowest:
    Item: potato,   Inventory Value: $3.25

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM