2011-12-05 32 views
0

可能重複的:
Looping in a spiral的Java創建一個螺旋

我創建一個程序一個3×3矩陣來填充。我想要看起來像這樣的東西

5 4 3 
6 1 2 
7 8 9 

正如你可能已經注意到它是一個螺旋。 現在我正在使用的算法是這樣的:我有一個二維數組,其中的值代表數字的座標。首先,我將這個數組中的每個數字座標的值都設爲10.然後從9開始,減小我的x座標,並將座標值賦給currentnum - 1,直到達到最終值或者它的值不是10;然後我做同樣的事情,除了增加Y的值;然後減小x的值; Y的;

我給每個數字指定10的原因非常類似於它作爲我的程序的道路。由於當前的數字不會超過9.如果一個正方形的值是10,它就像一個綠燈。如果它不是10意味着一個值被分配給那個方塊,那麼它就會脫離它。

這裏是我的代碼,請注意它是用Java編寫

public class spiral { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     int spiral [] [] = new int[3][3]; 
     for(int i = 0; i <= 2; i++){ 
      for(int j = 0; j <= 2; j++){ 
       spiral[i][j] = 10; 
      } 
     } 
     //0 is x value, 1 is y value 
     spiral[0][0] = 9; 
     int x = 1; 
     int y = 1; 
     int counter = 1; 
     int currentnum = 9; 
     int gridsquare = 3; 
     for(int i = 0; i <= 8; i++){ 

      if(counter == 5){ 
       counter = 1; 
      } 
      if(counter == 1){ 
       System.out.println(x + " " + y); 
       for(int j = 0;j <= 1;j++){ 
        if(spiral[x][y] == 10){ 
         spiral[x][y] = currentnum; 
         currentnum--; 
         x += 1; 
        } 
        else{ 
         y += 1; 
         break; 
        } 
       } 
      } 
      if(counter == 2){ 
       for(int k = 0; k <= 0; k++){ 
        System.out.print(x + " " + y); 
        if(spiral[x][y] == 10){ 
         spiral[x][y] = currentnum; 
         currentnum--; 
         y += 1; 
        } 
        else{ 
         x -= 1; 
         break; 
        } 
       } 
      } 
      if(counter == 3){ 
       for(int z = 0; z <= 0; z++){ 
        if(spiral[x][y] == 10){ 
         spiral[x][y] = currentnum; 
         currentnum--; 
         x -= 1; 
        } 
        else{ 
         y -= 1; 
         break; 
        } 
       } 
      } 
      if(counter == 4){ 
       for(int b = 0; b <= 0; b++){ 
        if(spiral[x][y] == 10){ 
         spiral[x][y] = currentnum; 
         currentnum--; 
         y -= 1; 
        } 
        else{ 
         x += 1; 
         break; 
        } 
       } 
      } 
      counter++; 
     } 
     System.out.print(currentnum); 
    } 
} 

我得到這個錯誤

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 
    at spiral.main(spiral.java:44) 

由於我是新來的Java會有人請提出一個更多鈔票修復爲了這。此外,如果您發現我的算法有任何問題,請通知我。

+3

嘗試與調試器追捕你的問題。對於這種問題真的很難分辨.... – Beginner

+0

我無法追捕它。:(也許是因爲我對調試器不太熟練 – Tom

回答

1

您不需要預先填寫10:0工作。

我認爲解決螺旋問題的最佳方法是考慮如何手動執行:從一個角落開始,水平走向,直到遇到非零或數組邊緣。然後你右轉。當前數字超過N * N時停止。

現在讓我們來看一下是什麼算法的每個部分是指:

  • 在邊角開始機構將x = 0和Y = 0。用直線表示x = x + dx,y = y + dy,其中dx或dy爲零,dy或dx爲1或-1。
  • 右轉意味着將dx分配給dy並將-dy分配給dx。

這裏是如何看起來代碼:

int current = 1; 
// Start in the corner 
int x = 0, y = 0, dx = 1, dy = 0; 
while (current <= N*N) { 
    // Go in a straight line 
    spiral[x][y] = current++; 
    int nx = x + dx, ny = y + dy; 
    // When you hit the edge... 
    if (nx < 0 || nx == N || ny < 0 || ny == N || spiral[nx][ny] != 0) { 
     // ...turn right 
     int t = dy; 
     dy = dx; 
     dx = -t; 
    } 
    x += dx; 
    y += dy; 
} 
0

您已將xy增加爲3,該值已超過您的某個數組的末尾。

使用調試器逐步執行程序,或者在每個if (counter)之前添加System.out.println語句以查明您正在執行此操作的位置。