找到标准偏差给我“

发布于 2025-01-22 15:10:13 字数 5054 浏览 0 评论 0原文

我的具体问题是我尝试计算标准偏差的地方。我查看了如何做到这一点,并在网上找到了可以使用的代码。当我实施并运行它时,我会发现错误。

线程“ main” java.lang.arayindexoutofboundsexception中的异常:index 10在class2.supermarkets.getstandereddeviation(supermarkets.java:73)的范围10中。 在class2.supermarkets.displaystandereddeviation(Supermarkets.java:89) 在class2.test.testsupermarket.main(testsupermarket.java:15)

IM新手,任何和所有帮助都非常感谢。

package classes2;
import java.text.NumberFormat;
import java.util.Locale;

public class Supermarkets {
    String names[];
    double profits[];
    double sum = 0, mean, n=10200000;
    NumberFormat formatter = NumberFormat.getCurrencyInstance(Locale.US);

    public Supermarkets(String[] names, double[] profits) {
        super();
        this.names = names;
        this.profits = profits;
    }

    public void displayStatus() {
        System.out.println("Original cities profit");
        System.out.printf("%-20s%s\n", "City", "Profit");
        String profit;
        for (int i = 0; i < names.length; i++) {
            profit = formatter.format(profits[i]);
            System.out.printf("%-20s%s\n", names[i], profit);
        }
        System.out.println();
    }

    public double getAverage() {
        Double totalProfit = 0.0;
        for (int i = 0; i < names.length; i++) {
            totalProfit += profits[i];
        }
        totalProfit = totalProfit / names.length;
        return totalProfit;
    }

    public void displayAverage() {
        double average = getAverage();
        System.out.println("Average profit for the Supermarket chain is " + formatter.format(average));
        System.out.println();
    }

    public void displayHighProfitCity() {
        double highProfit = 0.0;
        String highProfitCity = "";
        for (int i = 0; i < names.length; i++) {
            if (highProfit < profits[i]) {
                highProfit = profits[i];
                highProfitCity = names[i];
            }
        }
        System.out.println("Highest profit city is " + highProfitCity);
        System.out.println();
    }

    public void displayCityAboveAverageProfit() {
        double average = getAverage();
        System.out.println("Cities at or above average ");
        System.out.printf("%-20s%s\n", "City", "Profit");
        String profit;
        for (int i = 0; i < names.length; i++) {
            if (profits[i] >= average) {
                profit = formatter.format(profits[i]);
                System.out.printf("%-20s%s\n", names[i], profit);
            }
        }
        System.out.println();
    }

    public double getStanderedDeviation(){
        for(int i=0;i<n;i++)
        {
            sum=sum+profits[i];
        }
        mean=sum/n;
        System.out.println("Mean :"+mean);
        sum=0;
        for(int i=0;i<n;i++)
        {
            sum+=Math.pow((profits[i]-mean),2);

        }
        mean=sum/(n-1);
        double deviation=Math.sqrt(mean);
        return deviation;
    }

    public void displayStanderedDeviation() {
    double StanderedDeviation = getStanderedDeviation();
    System.out.println("the standard deviation is " + StanderedDeviation);
    System.out.println();
    }

        public void displayInfoInDescendingOrder () {
            String nameCopy[] = names;
            double profitCopy[] = profits;
            for (int i = 0; i < nameCopy.length; i++) {

                for (int j = i + 1; j < nameCopy.length; j++) {

                    if (profitCopy[i] > profitCopy[j]) {
                        double temp = profitCopy[i];
                        profitCopy[i] = profitCopy[j];
                        profitCopy[j] = temp;
                        String nameTemp = nameCopy[i];
                        nameCopy[i] = nameCopy[j];
                        nameCopy[j] = nameTemp;
                    }
                }
            }
            System.out.printf("%-20s%s\n", "City", "Profit");
            String profit;
            System.out.println("Cities and profit details is descending order");
            for (int i = 0; i < nameCopy.length; i++) {
                profit = formatter.format(profitCopy[i]);
                System.out.printf("%-20s%s\n", nameCopy[i], profit);
            }
            System.out.println();
        }
    }

这是我的主要班级


public class TestSupermarket {
    public static void main(String[] args) {
        String cities[] = {"Miami","Sunrise","Hollywood","Tallahassee",
                "Jacksonville","Orlando","Gainesville","Pensacola","Ocala","Sebring"
        };
        double profit[] ={10200000,14600000,17000000,6000000,21600000,
                9100000,8000000,12500000,2000000,4500000};
        classes2.Supermarkets markets =new classes2.Supermarkets(cities, profit);
        markets.displayStatus();
        markets.displayAverage();
        markets.displayHighProfitCity();
        markets.displayCityAboveAverageProfit();
        markets.displayStanderedDeviation();
        markets.displayInfoInDescendingOrder();
    }
}

my specific problem is where I try to calculate the Standard deviation. I looked up how to do this and found a code online that would work. when I implemented it and ran it I got the error.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10 at classes2.Supermarkets.getStanderedDeviation(Supermarkets.java:73)
at classes2.Supermarkets.displayStanderedDeviation(Supermarkets.java:89)
at classes2.TestSupermarket.main(TestSupermarket.java:15)

Im new to java and any and all help is greatly appreciated thank you.

package classes2;
import java.text.NumberFormat;
import java.util.Locale;

public class Supermarkets {
    String names[];
    double profits[];
    double sum = 0, mean, n=10200000;
    NumberFormat formatter = NumberFormat.getCurrencyInstance(Locale.US);

    public Supermarkets(String[] names, double[] profits) {
        super();
        this.names = names;
        this.profits = profits;
    }

    public void displayStatus() {
        System.out.println("Original cities profit");
        System.out.printf("%-20s%s\n", "City", "Profit");
        String profit;
        for (int i = 0; i < names.length; i++) {
            profit = formatter.format(profits[i]);
            System.out.printf("%-20s%s\n", names[i], profit);
        }
        System.out.println();
    }

    public double getAverage() {
        Double totalProfit = 0.0;
        for (int i = 0; i < names.length; i++) {
            totalProfit += profits[i];
        }
        totalProfit = totalProfit / names.length;
        return totalProfit;
    }

    public void displayAverage() {
        double average = getAverage();
        System.out.println("Average profit for the Supermarket chain is " + formatter.format(average));
        System.out.println();
    }

    public void displayHighProfitCity() {
        double highProfit = 0.0;
        String highProfitCity = "";
        for (int i = 0; i < names.length; i++) {
            if (highProfit < profits[i]) {
                highProfit = profits[i];
                highProfitCity = names[i];
            }
        }
        System.out.println("Highest profit city is " + highProfitCity);
        System.out.println();
    }

    public void displayCityAboveAverageProfit() {
        double average = getAverage();
        System.out.println("Cities at or above average ");
        System.out.printf("%-20s%s\n", "City", "Profit");
        String profit;
        for (int i = 0; i < names.length; i++) {
            if (profits[i] >= average) {
                profit = formatter.format(profits[i]);
                System.out.printf("%-20s%s\n", names[i], profit);
            }
        }
        System.out.println();
    }

    public double getStanderedDeviation(){
        for(int i=0;i<n;i++)
        {
            sum=sum+profits[i];
        }
        mean=sum/n;
        System.out.println("Mean :"+mean);
        sum=0;
        for(int i=0;i<n;i++)
        {
            sum+=Math.pow((profits[i]-mean),2);

        }
        mean=sum/(n-1);
        double deviation=Math.sqrt(mean);
        return deviation;
    }

    public void displayStanderedDeviation() {
    double StanderedDeviation = getStanderedDeviation();
    System.out.println("the standard deviation is " + StanderedDeviation);
    System.out.println();
    }

        public void displayInfoInDescendingOrder () {
            String nameCopy[] = names;
            double profitCopy[] = profits;
            for (int i = 0; i < nameCopy.length; i++) {

                for (int j = i + 1; j < nameCopy.length; j++) {

                    if (profitCopy[i] > profitCopy[j]) {
                        double temp = profitCopy[i];
                        profitCopy[i] = profitCopy[j];
                        profitCopy[j] = temp;
                        String nameTemp = nameCopy[i];
                        nameCopy[i] = nameCopy[j];
                        nameCopy[j] = nameTemp;
                    }
                }
            }
            System.out.printf("%-20s%s\n", "City", "Profit");
            String profit;
            System.out.println("Cities and profit details is descending order");
            for (int i = 0; i < nameCopy.length; i++) {
                profit = formatter.format(profitCopy[i]);
                System.out.printf("%-20s%s\n", nameCopy[i], profit);
            }
            System.out.println();
        }
    }

this is my main class


public class TestSupermarket {
    public static void main(String[] args) {
        String cities[] = {"Miami","Sunrise","Hollywood","Tallahassee",
                "Jacksonville","Orlando","Gainesville","Pensacola","Ocala","Sebring"
        };
        double profit[] ={10200000,14600000,17000000,6000000,21600000,
                9100000,8000000,12500000,2000000,4500000};
        classes2.Supermarkets markets =new classes2.Supermarkets(cities, profit);
        markets.displayStatus();
        markets.displayAverage();
        markets.displayHighProfitCity();
        markets.displayCityAboveAverageProfit();
        markets.displayStanderedDeviation();
        markets.displayInfoInDescendingOrder();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╄→承喏 2025-01-29 15:10:13

您尝试循环10,200,000次,但只有10个利润值。如果您想通过利润循环,请使用“ i&lt; profits.length”而不是此全局变量。

祝您爪哇旅程好运。

You try to loop 10,200,000 times but you only have 10 profit values. If you want to loop through profits, use "i < profits.length" instead of this global variable.

Good luck on your Java journey.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文