找到平均价格?

63lcw9qa  于 2021-07-08  发布在  Java
关注(0)|答案(3)|浏览(286)

我必须存储在二维数组中的数据,然后打印价格的平均值,其中有可用的选项“否”,任何人都可以帮助找到平均值。

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);
    }
}
ocebsuys

ocebsuys1#

您不需要定义二维数组,只需维护产品的一维数组,并维护需要考虑平均值的合格项的计数(在您的情况下是不可用的项)
此代码将在单个循环中运行,而不需要嵌套循环

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);
    }
}

产量-平均价格:4049.5

tag5nh1u

tag5nh1u2#

你需要定义一个 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);
vuktfyat

vuktfyat3#

如果您使用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

如你所见,它很简洁。我在这里用过小溪。流有助于减少代码的冗长。

相关问题