Elevens Activity 4 APCS调试

时间:2016-03-20 20:52:36

标签: java

我是APCS的初学者程序员,我在我的代码上收到此错误消息(如下)。我该如何解决?我不确定它有什么问题,因为我相信所有被调用的变量都在代码的范围内,我宣称它们是公开的...让我知道我应该寻找什么,有什么不对的!非常感谢你。

错误:

java.lang.ArrayIndexOutOfBoundsException: 4
at Shuffler.perfectShuffle(Shuffler.java:49)
at Shuffler.main(Shuffler.java:16)

代码:

import java.util.ArrayList;
public class Shuffler
{

   private static final int SHUFFLE_COUNT = 1;

   private static final int VALUE_COUNT = 4;

  public static void main (String[] args)
  {
      System.out.println ("Results of " + SHUFFLE_COUNT + " consecutive perfect shuffles:");
      int[] values1 = new int[VALUE_COUNT];
      for (int i = 0; i < values1.length; i++)
      {
          values1 [i] = i;
      }
      for (int j = 1; j <= SHUFFLE_COUNT; j++)
      {
          perfectShuffle (values1);
          System.out.print(" " + j + ":");
          for (int k = 0; k < values1.length; k++)
          {
              System.out.println (" " + values1 [k]);
          }
          System.out.println();
      }
      System.out.println();
      System.out.println("Results of " + SHUFFLE_COUNT + " consecutive efficient selection shuffles:");
      int[] values2 = new int[VALUE_COUNT];
      for (int i = 0; i < values2.length; i++) 
      {
          values2[i] = i;
      }
      for (int j = 1; j <= SHUFFLE_COUNT; j++) 
      {
          selectionShuffle(values2);
          System.out.print(" " + j + ":");
          for (int k = 0; k < values2.length; k++) 
          {
              System.out.print(" " + values2[k]);
          }
          System.out.println();
      }
      System.out.println();
  }

  public static void perfectShuffle (int[] values)
  {
      int[] temp = new int [52];
      int k = 0;
      for (int j = 0; j < 25; j++)
      {
          temp [k] = values [j];
          k+=2;
      }
      k=1;
      for (int j=26; j<values.length; j++)
      {
          temp [k] = values [j];
          k+=2;
      }
      for (int j=0; j<values.length; j++)
      {
          values [j] = temp [j];
      }
  }

  public static void selectionShuffle (int[] values) 
  {
      ArrayList<Integer> temp=new ArrayList<Integer>(52);
      int rando=(int) Math.random()*52;
      for (int counter=0; counter<temp.size(); counter++)
      {
          temp.set (counter,rando);
        }
    }
}

0 个答案:

没有答案