2012-04-15 46 views
1

我的indexoutofbounds問題已解決,但不是我的程序編譯,但打印出一個未改變的難題..不知道我要去哪裏錯了?最初的拼圖讀取ROM標準輸入在0數獨拼圖的'空'單元的地方。我也包括了我的數據列表初始化程序。java數獨解算器值不變

public ArrayList<Integer> create(){ 

    ArrayList<Integer> possible = new ArrayList<Integer>(); 

    for(int i=1; i<10; i++){ 
     possible.add(i); 
    } 
    return possible; 
    } 
    public sudoku(int size) 
    { 
    SIZE = size; 
    N = size*size; 

    Grid = new int[N][N]; 
    for(int i = 0; i < N; i++) 
     for(int j = 0; j < N; j++) 
      Grid[i][j] = 0; 
    } 

    public void solve() 
    { 
    int a, b, c, d, i, j, k, l; 

    int count = 0; 
    int value= 0; 

    for(i=0; i<N;i++){ 
     for(j=0; j<N;j++){ 
      if(Grid[i][j]==0){ 

       ArrayList<Integer> possible = create(); 

       //check row    
       for(a=0; a<N;a++){ 
        for(b=0; b<N; b++){ 
         if(Grid[a][0]==possible.get(a)){ 
          possible.set(a, 0); 
         } 
        } 
       } 
       //check column 
       for(c=0; c<N;c++){ 
        for(d=0; d<N;d++){ 
         if(Grid[0][d]==possible.get(d)){ 
          possible.set(d,0); 
         } 
        } 
       } 
       for(k=0; k<9; k++){ 
        if(possible.get(k)!=0){ 
         count++; 
        } 
       } 
       if(count==1){ 
        for(l=0; l<9; l++){ 
         if(possible.get(l)!=0){ 
          value=possible.get(l); 
         } 
        } 
       } 
       Grid[i][j]=value; 
      } 
     } 
    } 
} 
+0

我們看不到初始化網格大小的位置,所以幫助您更加困難。 (或者可能不可能幫到你) – 2012-04-15 03:52:09

+1

我並不是真正興奮的_Grid [我] [0] _,我看到了幾次......這不是你的問題......但。 – 2012-04-15 03:52:49

+1

哪條線路不通? – 2012-04-15 03:54:03

回答

2

我看到您的問題,您使用的i和j變量指數不止一次在嵌套的for循環:

for (i = 0; i < N; i++) { // **** you use "i" it here 
    for (j = 0; j < N; j++) { // **** and "j" here 
     if (Grid[i][j] == 0) { 

      ArrayList<Integer> possible = create(); 

      for (i = 0; i < N; i++) { // **** and again here 
       for (j = 0; j < N; j++) { // **** and again here 
       if (Grid[i][0] == possible.get(i)) { 
        possible.set(i, 0); 
       } 
       } 
      } 

      for (i = 0; i < N; i++) { // **** and again here 
       for (j = 0; j < N; j++) { // **** and again here 
       if (Grid[0][j] == possible.get(i)) { 
        possible.set(i, 0); 
       } 
       } 
      } 

      // .... 

      Grid[i][j] = value; 
     } 
    } 
    } 

通過從內推動指數環,你的風險超過最大指數,所以當你到達底部時,你的i和j一直增加到9,超過了行和列的大小。您幾乎不應該在for循環中更改for循環索引。您將需要重新編寫此代碼。

編輯:它甚至比這更簡單:您在for循環結束後檢查我,以便我是上限的值。運行這個來看看我的意思:

for (i = 0; i < N; i++) { 
    for (i = 0; i < N; i++) { 
     System.out.println("C) i = " + i); 
    } 
    System.out.println("D) i = " + i); 
    } 
+0

哦,我明白了!這很愚蠢。非常感謝 – user1205722 2012-04-15 04:02:50

+0

對於每個循環,i和j都被設置爲零,所以我沒有看到錯誤在哪裏,你能更具體嗎?或者你是否說過在最後一個陳述中使用的i和j不是他打算使用的那個? (即它們是來自最近for循環而不是第一個?) – 2012-04-15 04:03:19

+0

實際上,他甚至不需要嵌套我,但在for循環結束後,index變量是循環的最大值。 – 2012-04-15 04:07:50

0

嘗試打印possible.get(0)以確保它不是空的!如果這會引發錯誤,那麼你就去!或者,你可以在循環之間嘗試一個try語句來找出哪個部分拋出它,如下所示。

try{ 
    ArrayList<Integer> possible = create(); 
} 
catch(ArrayIndexOutOfBoundsException e){ 
     System.out.println(1); 
} 
try{ 
       //check row    
       for(i=0; i<N;i++){ 
        for(j=0; j<N;j++){ 
         if(Grid[i][0]==possible.get(i)){ 
          possible.set(i, 0); 
         } 
        } 
       } 
} 
catch(ArrayIndexOutOfBoundsException e){ 
     System.out.println(2); 
} 
try{ 
       //check column 
       for(i=0; i<N;i++){ 
        for(j=0; j<N;j++){ 
         if(Grid[0][j]==possible.get(i)){ 
          possible.set(i,0); 
         } 
        } 
       } 
} 
catch(ArrayIndexOutOfBoundsException e){ 
     System.out.println(3); 
} 
try{ 
       for(k=0; k<9; k++){ 
        if(possible.get(k)!=0){ 
         count++; 
        } 
       } 
} 
catch(ArrayIndexOutOfBoundsException e){ 
     System.out.println(4); 
} 
try{ 
       if(count==1){ 
        for(l=0; l<9; l++){ 
         if(possible.get(l)!=0){ 
          value=possible.get(l); 
         } 
        } 
       } 
} 
catch(ArrayIndexOutOfBoundsException e){ 
     System.out.println(5); 
} 
try{ 
       Grid[i][j]=value;} 
catch(ArrayIndexOutOfBoundsException e){ 
     System.out.println(6); 
} 
+0

這是不必要的 - 一個異常的堆棧跟蹤會準確地告訴你它從哪裏拋出。 – 2012-04-15 04:14:24

+0

我的程序編譯但數獨謎題(技術上爲0)中的空值不會改變 – user1205722 2012-04-15 04:17:32