找到标准偏差给我“
我的具体问题是我尝试计算标准偏差的地方。我查看了如何做到这一点,并在网上找到了可以使用的代码。当我实施并运行它时,我会发现错误。
线程“ 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尝试循环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.