java.lang.ArrayIndexOutOfBoundsException :10
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23034799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.ArrayIndexOutOfBoundsException :10
提问by TheJavaLEarner
I am a newbie here and I try to write a program that will be calculating the deviation of 10 numbers in the array, here is my code that I got created :
我是这里的新手,我尝试编写一个程序来计算数组中 10 个数字的偏差,这是我创建的代码:
package week10;
import java.util.Scanner;
public class deviation {
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
double testScores[] = new double [10];
double sum = 0;
int count = 0;
int count2 = 0;
int count3 = 0;
double inputDouble = 0;
int arrayIndex = 0;
//GET INPUT TEST SCORES
while(inputDouble >= 0) {
System.out.print("Enter Score: ");
inputDouble = input.nextDouble();
if(inputDouble >= 0)
{
testScores[arrayIndex] = inputDouble;
sum += inputDouble;
}
arrayIndex++;
}
if(arrayIndex < testScores.length)
{
for (int x = arrayIndex-1; x <= testScores.length-1; x++)
{
testScores[x] = -1;
}
}
//GET NEW ARRAY WITH ONLY VALID SCORES
double[] validScores = GetValidScores(testScores, arrayIndex-1);
System.out.println(" The mean is: " + mean(validScores));
System.out.println(" The standard deviation is: " + deviation(validScores));
}
private static double[] GetValidScores(double[] inputArray, int validScoreCount) {
double[] newScores = new double[validScoreCount];
for(int z = 0; z < validScoreCount; z++)
{
newScores[z] = inputArray[z];
}
return newScores;
}
public static double deviation(double[] values) {
double sum = 0.00;
double theMean = mean(values);
for(int i =0; i < values.length; i++) {
double currentCalc = values[i] - theMean;
sum += Math.pow(currentCalc, 2);
}
sum = sum / (values.length -1);
sum = Math.sqrt(sum);
return sum;
}
public static double mean(double[] values)
{
double sum = 0.00;
for(int i=0; i < values.length; i++)
{
sum += values[i];
}
return sum / values.length;
}
}
Output:
输出:
Enter Score: 25
Enter Score: 25
Enter Score: 25
Enter Score: 25
Enter Score: 25
Enter Score: 25
Enter Score: 25
Enter Score: 25
Enter Score: 25
Enter Score: 25
Enter Score: 25
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at week10.deviation.main(deviation.java:26)"
I understand that array is 10, but it starts with a 0, so this is why I chose command array-1, can you please tell me or show what am I doing wrong?
我知道数组是 10,但它以 0 开头,所以这就是我选择命令 array-1 的原因,你能告诉我或显示我做错了什么吗?
采纳答案by JB Nizet
You have an array of 10 elements, and ask for the next element, in a loop, until the user enters a negative value. Since the user always enters 25, the loop never stops, until it reaches the 11th element. Accessing the 11th element (at index 10) of an array of 10 elementts throws the exception you're seeing.
您有一个包含 10 个元素的数组,并在循环中请求下一个元素,直到用户输入负值。由于用户总是输入 25,循环永远不会停止,直到它到达第 11 个元素。访问包含 10 个元素的数组的第 11 个元素(索引 10)会引发您所看到的异常。
回答by GoldRoger
If you want to read exactly 10 numbers, change reading input part like this
如果您想准确读取 10 个数字,请像这样更改读取输入部分
//GET INPUT TEST SCORES
for(int i=0;i<10;i++)
System.out.print("Enter Score: ");
inputDouble = input.nextDouble();
testScores[i] = inputDouble;
sum += inputDouble;
}