2016-12-11 2 views
-2

あなたが私のコードのいくつかで私を助けることができるかどうか疑問に思っていました。私はこのパズルクリエータをプログラミングしていましたが、私の二次元配列の値を生成しようとすると、OutOfBoundsExceptionが発生します。私はこのソリューションは簡単だと確信していますが、何らかの理由で何が間違っているのか分からないようです。 (また、当然のことながら、そこにこれに沿って行くドライバーのクラスですが、私はそれがここに含める必要はないと思う。)2次元ループOutOfBoundsException - Java

import java.util.*; 

public class ThirtyWonderful 
{ 
    private int dimen; 

    public ThirtyWonderful (int dimensions) 
    { 
     dimen = dimensions; 
    } 

    public ThirtyWonderful() 
    { 
     dimen = 5; 
    } 

    Random gen = new Random(); 
    int[][] nums = new int [dimen][dimen]; 

    public void genPuzzle() 
    { 
     for (int count = 0; count < dimen; count++) 
     { 
      for(int col = 0; col < dimen; col++) 
      { 
       nums[count][col] = gen.nextInt(9) + 1; 
      } 
     } 
     checkAcc(); 

     if(checkAcc() == true) 
     { 
      for (int count = 0; count < dimen; count++) 
      {  
       for(int col = 0; col < dimen; col++) 
       { 
        System.out.print(nums[count][col] + " "); 
       } 

       System.out.println(); 
      } 
     } 
    } 

    public boolean checkAcc() 
    { 
     int tot = 0; 
     for (int count = 0; count < dimen; count++) 
     { 
      for(int col = 0; col < dimen; col++) 
      { 
       tot += nums[count][col]; 
      } 

      if(tot != 31) 
       return false; 
     } 

     for (int count = 0; count < dimen; count++) 
     { 
      for(int col = 0; col < dimen; col++) 
      { 
       tot += nums[count][col]; 
      } 

      if(tot != 31) 
       return false; 
     } 

     return true; 
} 

}あなたは[新しいint型とNUMSを宣言している

+1

例外はどこにありますか? – Guy

答えて

0

dimen] [dimen]; NUMSは、新しいint型で初期化されるようにDIMENでも初期化する前に、デフォルトではDIMEN値は、0である[0] [0]ここで

は、修正コード

public class ThirtyWonderful { 

    private int dimen; 

    Random gen = new Random(); 
    int[][] nums; 

    public ThirtyWonderful(int dimensions) { 
     this.dimen = dimensions; 
     nums = new int[dimen][dimen]; 
    } 

    public ThirtyWonderful() { 
     this(5); 
    } 

    public void genPuzzle() { 
     for (int count = 0; count < dimen; count++) { 
      for (int col = 0; col < dimen; col++) { 
       nums[count][col] = gen.nextInt(9) + 1; 
      } 
     } 
     checkAcc(); 

     if (checkAcc() == true) { 
      for (int count = 0; count < dimen; count++) { 
       for (int col = 0; col < dimen; col++) { 
        System.out.print(nums[count][col] + " "); 
       } 

       System.out.println(); 
      } 
     } 
    } 

    public boolean checkAcc() { 
     int tot = 0; 
     for (int count = 0; count < dimen; count++) { 
      for (int col = 0; col < dimen; col++) { 
       tot += nums[count][col]; 
      } 

      if (tot != 31) { 
       return false; 
      } 
     } 

     for (int count = 0; count < dimen; count++) { 
      for (int col = 0; col < dimen; col++) { 
       tot += nums[count][col]; 
      } 

      if (tot != 31) { 
       return false; 
      } 
     } 

     return true; 
    } 

} 
+0

ありがとう!もちろん、私のプログラムは論理的なエラーのために仕事をしません...しかしそれは別の問題ですXD – TheFiveHundredYears

0

はあなたのコンストラクタであなたの配列オブジェクトを初期化する必要がありますです。オブジェクトがインスタンス化される前にクラスのロード時に配列を初期化しています。