For Loop未正常运行

时间:2015-12-01 19:27:42

标签: java for-loop

代码的情况是我认为私有方法中的for循环没有得到int [] userArray。我花了好几个小时试图找出为什么for循环不起作用,我甚至尝试使用while循环。

public class Arrays {

  static Scanner scan = new Scanner(System.in);
  private static int sizeOfArrayWanted;
  private static double average = 0;
  /**
   * private static int smallestNumber=1;
   * 
   * private static int count = 0; private static int total=0; private static
   * int average=total/count; private static int largestNumber=0; private static
   * int standardDeviation=0; private static int meanOfSD=0; private static int
   * x1=0; private static int[] userArray= new int [0];
   **/
  private static int total1 = 0;
  private static int standardDeviation = 0;
  private static int finalAverage = 0;

  public static void main(String[] args) {
    // int randomNumber = (int)((Math.random() * 9) + 1);

    System.out.println("Hello how big would you like the array?");
    int sizeOfArrayWanted = scan.nextInt();
    int smallestNumber = 1;
    int largestNumber = 0;
    int total = 0;
    int standardDeviation = 0;
    int count = 0;
    double average = 0;
    int[] userArray = new int[sizeOfArrayWanted];
    for (int x = 0; x < sizeOfArrayWanted; x++) {

      userArray[x] = (int) ((Math.random() * 9) + 0);
      total = total + userArray[x];
      count++;
      if (userArray[x] < smallestNumber) {
        smallestNumber = userArray[x];
      }
      if (userArray[x] > largestNumber) {
        largestNumber = userArray[x];
      }
      System.out.print(userArray[x] + ", ");

      average = total / count;

    }

    System.out.println("");

    System.out.println("largest Number= " + largestNumber);
    System.out.println("smallest Number= " + smallestNumber);
    System.out.println("average =" + (total / count));

    Arrays.computeStandardDeviation(userArray, sizeOfArrayWanted);
  }

  private static int computeStandardDeviation(int[] userArray, int sizeofArrayWanted) {

    int x1 = 0;
    while (x1 < sizeofArrayWanted) {
      x1++;
      userArray[x1] = (int) (userArray[x1] - average);
      userArray[x1] = (int) Math.pow(userArray[x1], 2);
      total1 = total1 + userArray[x1];
      finalAverage = total1 / x1;
      standardDeviation = (int) Math.sqrt(finalAverage);
    }

    return standardDeviation;
  }
}

当我尝试编译程序时,出现以下错误: 线程“main”中的异常最大Number = 6 最小数= 1 平均= 4

java.lang.ArrayIndexOutOfBoundsException: 5
    at Arrays.computeStandardDeviation(Arrays.java:80)
    at Arrays.main(Arrays.java:67)

3 个答案:

答案 0 :(得分:0)

数组索引超出范围异常是因为您在执行操作之前递增x1,而是应该执行所有操作并在结尾处递增x1。否则,在循环的最后一次迭代中,您将访问数组范围之外的元素。

private static int computeStandardDeviation(int[] userArray, int sizeofArrayWanted) {
    int x1 = 0;
    while (x1 < sizeofArrayWanted) {
        userArray[x1] = (int) (userArray[x1] - average);
        userArray[x1] = (int) Math.pow(userArray[x1], 2);
        total1 = total1 + userArray[x1];
        finalAverage = total1 / x1;
        standardDeviation = (int) Math.sqrt(finalAverage);

        x1++; //Increment should be done after all operations
    }

    return standardDeviation;
}

此外,您不需要在此处显式传递size参数,因为您可以从传递给方法的数组中获取大小。

答案 1 :(得分:0)

检查后你增加 x1。在最后一次迭代中使它对于数组来说太大了。将x1 ++移动到平均值计算中。

 while(x1<sizeofArrayWanted)
            {
          //  x1++;

     finalAverage = total1 / ++x1;

答案 2 :(得分:0)

问题来自computeStandardDeviation方法。索引从0开始,因此立即增加它将导致它不查看数组中的第一个点,因此也检查last index + 1位置。它应该是这样的

private static int  computeStandardDeviation(int [] userArray, int sizeofArrayWanted)
{
    int x1=0;
    while(x1<sizeofArrayWanted)
    {
        userArray[x1]=(int) (userArray[x1]-average);
        userArray[x1]=(int) Math.pow(userArray[x1], 2);
        total1=total1+userArray[x1];
        finalAverage=total1/x1;
        standardDeviation= (int) Math.sqrt(finalAverage);

        x1++; // move this here
    }

    return standardDeviation;
}