2017-04-23 77 views
-5

任務方矩陣中的所有元素是以下:輸出以螺旋

M階的方矩陣A被給出。從元素A0,0開始並順時針移動,您應該以螺旋形輸出所有元素:第一行,最後一列,倒序的最後一行,倒序第一列,第二行的其餘元素等等。

public class Pres10Task8 { 

    public static void main(String[] args) { 
     int m =4; 

     int [][] a=new int [m][m]; 
     Random rand = new Random(); 
     for(int i =0;i<a.length;i++){ 
      for(int j =0;j<a[i].length;j++){ 
       a[i][j]=rand.nextInt(100); 
       System.out.print(a[i][j]+" "); 
      } 
      System.out.println(); 
     } 
     for(int k=0;k<m/2+1;k++){ 
      for(int j = k; j<m+1-k;j++){ 
       System.out.println(a[k][j]); 
      } 
      int j =m+1-k; 
      for(int i=k+1;i<m+1-k;i++){    
       System.out.println(a[i][j]); 
      } 

      for(int j=m-k;j>k;j--){ 
       j =k ; 
       System.out.println(a[i][j]); 
      } 
      for(int i =m-k;i>k+1;i--){ 
       i =m+1-k; 
       System.out.println(a[i][j]); 
      } 
     } 
    } 

} 

你會這麼好心通過我的代碼看,說有什麼不對呢?我應該如何重寫我的代碼才能獲得正確的輸出結果?

+3

你能解釋一下是什麼讓你認爲你的代碼是不正確的?你會得到錯誤,異常,結果不正確嗎? – Pshemo

+0

歡迎來到Stack Overflow!請[參觀](http://stackoverflow.com/tour)以查看網站的工作原理和問題,並相應地編輯您的問題。另請參閱:[如何創建最小,完整和可驗證的示例](http://stackoverflow.com/help/mcve) –

+0

爲什麼使用隨機 - 您將如何看到它以螺旋方式填充? 爲什麼交錯數組而不是二維? 我發佈了一個初始化樣本,並以可讀的方式打印出任何數組。 –

回答

0

你不能在你的循環使用一個變量的方法之前已經存在:

int j = m + 1 - k; 
// ^--------------------------------------Already exist 
for (int i = k + 1; i < m + 1 - k; i++) { 
    System.out.println(a[i][j]); 
} 

for (int j = m - k; j > k; j--) { 
//  ^---------You can't declare a variable already exist, 
        //you can just use it or initialize it 

因此,而不是使用這個不int j

for (j = m - k; j > k; j--) { 

System.out.println(a[i][j]); 
//     ^-----The i is not exist so you have to 
         //create it and inisialize it so you can use it 
+2

謝謝你的回答。我糾正了這些錯誤。 – Vit

+0

謝謝你的回答。我糾正了這些錯誤。但仍然關於第二個錯誤。我應該啓動它爲int i = 0或者可能是這樣的(int i = 0; i Vit

+0

@Vit我不知道你想做什麼,我只是引導你糾正你的錯誤,你的算法的邏輯是另一個問題,檢查Sanket Makani的答案也許這是你想要的 –

0

作爲YCF_L男人你犯了很多錯誤在答案中,也解決了其中幾個我仍然得到其他錯誤。您可以通過一個簡單的邏輯來解決這個問題,即Divide Square Matrix轉換爲smaller squares,然後按照相同的模式打印每個squares

例如

假設我有順序5的矩陣然後我們可以有3 squares此處不同的維度。

1 1 1 1 1 
1 2 2 2 1 
1 2 3 2 1 
1 2 2 2 1 
1 1 1 1 1 

在此,每個數字代表,其正方形的element belongs.So所有外元件屬於first square然後下一層的元件屬於second square等。

現在,您已經通過將問題分爲更小的sub-problems來解決問題,這可以通過相同的方式解決。現在您需要製作打印每個squares的元素的邏輯。

因此,考慮最外面的正方形。

1 1 1 1 1          1 2 3 4 5 
1  1  Thier Printing Order    16   6 
1  1  ================>    15   7 
1  1          14   8 
1 1 1 1 1          13 12 11 10 9 

所以注意到我們startfirst element,並轉入same rowright direction

然後移動到same columndown direction

然後再次在same rowleft direction

最後在same column但在up direction

打印之後outer square轉到inner square並對squares中的每一個都做同樣的處理。

代碼相同:

import java.util.Scanner; 
import java.util.Random; 

public class Main { 

    public static void main(String[] args) { 

     int m =4; 

     int [][] a=new int [m][m]; 
     Random rand = new Random(); 
     for(int i =0;i<a.length;i++){ 
      for(int j =0;j<a[i].length;j++){ 
       a[i][j]=rand.nextInt(100); 
       System.out.print(a[i][j]+" "); 
      } 
      System.out.println(); 
     } 

     int squares=m/2;        //Calculating total number of squares 

     for(int i=0;i<squares;i++) 
     { 
      int low=i;        //Set the dimension of the square 
      int high=m-i-1; 

      for(int j=low;j<=high;j++)    //First Row --> (Right Direction) 
       System.out.println(a[low][j]); 
      for(int j=low+1;j<=high;j++)   //Last Column --> (Down Direction) 
       System.out.println(a[j][high]); 
      for(int j=high-1;j>=low;j--)   //Last Row --> (Left Direction) 
       System.out.println(a[high][j]); 
      for(int j=high-1;j>low;j--)   //First Column --> (Up Direction) 
       System.out.println(a[j][low]); 
     } 

     if(m%2==1)         //If Matrix is of odd order then print the middle element. 
      System.out.println(a[mid][mid]); 
    } 

} 
0

爲什麼您使用交錯的數組[][],而不是2維[,] ??? 可以舉一個簡單的例子:

public void CountDiag(int size) 
    { 
     // initialize straight order 
     int[,] ar2 = new int[size, size]; 
     int count = 0; 
     // initialize spiral way 
     int y = 0; 
     int x = 0; 
     int top = 0; 
     int bot = ar2.GetLength(0)-1; 
     int left = 0; 
     int right = ar2.GetLength(1)-1; 
     do 
     { 
      //topleft to right 
      for (; y < right; y++) 
      { 
       ar2[x, y] = count + 1; 
       count++; 
      } 
      ar2[x, y] = count + 1;     
      right--; 
      //topright to bottom 
      for (; x < bot; x++) 
      { 
       ar2[x, y] = count + 1; 
       count++; 
      } 
      ar2[x, y] = count + 1;     
      top++; 
      //botright to left 
      for (; y > left; y--) 
      { 
       ar2[x, y] = count + 1; 
       count++; 
      } 
      ar2[x, y] = count + 1; 
      left++; 
      //botleft to top 
      for (; x > top; x--) 
      { 
       ar2[x, y] = count + 1; 
       count++; 
      } 
      ar2[x, y] = count + 1; 
      bot--; 
     } while (count < ar2.Length-1); 

}

,則在打印出來是這樣的:

public void PrintArray(int[,] array) 
    { 
     int n = (array.GetLength(0) * array.GetLength(1) - 1).ToString().Length + 1; // for padding 

     for (int i = 0; i < array.GetLength(0); i++) // 0 - length rows 
     { 
      for (int j = 0; j < array.GetLength(1); j++) // 1 length columns 
      { 
       Console.Write(array[i, j].ToString().PadLeft(n, ' ')); 
      } 
      Console.WriteLine(); 
     } 
     Console.ReadLine(); 
    }