簡體   English   中英

我如何找到價格的平均值?

[英]How do I find the average of the price?

我必須將數據存儲在二維數組中,然后打印具有可用選項“否”的價格的平均值,任何人都可以幫助找到平均值。

public class Test {
    public static void main(String[] args) {
        double total = 0;
        Products[][] names = new Products[][]{
                {new Products("k7580", 6500, "yes")},
                {new Products("k8570", 7333, "no")},
                {new Products("k8580", 766, "no")},
                {new Products("k6580", 288, "yes")}
        };
        for (int i = 0; i < names.length; i++) {
            for (int j = 0; j < names[i].length; j++) {
                String available = names[i][j].getAvailable();
                if (available.equals("no")) {
                    total = total + names[i][j].getPrice();
                }
            }
            System.out.println("");
        }
        // from here I am not able to find average
        double average = total / names.length;
        System.out.println("average of price :" + average);
    }
}

您需要定義一個count變量,根據您的 if 條件( available.equals("no") )每次遞增它以僅計算滿足條件的元素:

double total = 0;
Products[][] names = new Products[][] { { new Products("k7580", 6500, "yes") },
        { new Products("k8570", 7333, "no") }, { new Products("k8580", 766, "no") },
        { new Products("k6580", 288, "yes") } };

int count = 0;
for (int i = 0; i < names.length; i++) {
    for (int j = 0; j < names[i].length; j++) {
        String available = names[i][j].getAvailable();
        if (available.equals("no")) {
            count++;
            total +=names[i][j].getPrice();
        }
    }
    System.out.println("");
}

// from here I am not able to find average

double average = total / count;
System.out.println("average of price :" + average);

如果您使用Java 8或更高版本,則可以使用它:

public static void main(String[] args) {
    Products[][] names = new Products[][]{
            {new Products("k7580", 6500, "yes")},
            {new Products("k8570", 7333, "no")},
            {new Products("k8580", 766, "no")},
            {new Products("k6580", 288, "yes")}
    };

    Arrays.stream(names)
            .flatMap(array -> Arrays.stream(array))
            .filter(products -> products.getAvailable().equals("no"))
            .mapToDouble(Products::getPrice)
            .average()
            .ifPresent(average -> System.out.println(
                    "average of price: " + average));
}

輸出是:

average of price: 4049.5

如您所見,它很簡潔。 我在這里使用了 Stream 有助於減少代碼的冗長。

您不需要定義二維數組,只需維護一維產品數組並維護需要考慮平均的合格商品的數量(在您的情況下不可用的商品)

此代碼將在單個循環中運行,無需嵌套循環。

public class Test {
    public static void main(String[] args) {
        double total = 0;
        int notAvailableItems = 0;
        Products[] names = new Products[]{
                new Products("k7580", 6500, "yes"),
                new Products("k8570", 7333, "no"),
                new Products("k8580", 766, "no"),
                new Products("k6580", 288, "yes")};
        for (int i = 0; i < names.length; i++) {
            Products product = names[i];
            if (product.getAvailable().equals("no")) {
                total = total + product.getPrice();
                notAvailableItems++;
            }
        }
        double average = total / (double) notAvailableItems;
        System.out.println("average of price: " + average);
    }
}

輸出:

average of price: 4049.5

暫無
暫無

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

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