2017-03-16 54 views
0

我想在一個方形矩陣中顯示一個數字數組,在一個蛇形圖案中增加1。不能得到正確的輸出。用戶輸入行/列和矩陣顯示。看下面的照片。我也嘗試過如果使用模數的偶數/奇數行的語句,但仍然得到相同的輸出。 (多多包涵,我是新來這個,關於格式抱歉,或者如果我失去了信息)循環不給我想要的矩陣輸出

http://imgur.com/a/ZDhFw

import java.util.Scanner; 



public class A3_Q2 { 

public static void main(String[] args) 
{ 
    // TODO Auto-generated method stub 

    Scanner keyboard = new Scanner(System.in); 

    System.out.println("[-------------------------]"); 
    System.out.println("[  Array Pattern  ]"); 
    System.out.println("[-------------------------]"); 

    System.out.println("How many rows/columns do you want your array to have? (Mist be at least 3):"); 


    int arraySize = keyboard.nextInt(); 

    while(arraySize < 3) 
    { 
     System.out.println("Lets try this again ...."); 
     System.out.println("How many rows/columns do you want your array to have? (Mist be at least 3):"); 
     arraySize = keyboard.nextInt(); 
    } 


    int [][] pattern = new int[arraySize][arraySize]; 

    int i = 0; 
    int number = 1; 

    while (i < arraySize) 
    { 
     for (int j = 0; j < arraySize; ++j) 
     { 
      pattern[i][j] = number; 
      System.out.printf("%3d", pattern[i][j]); 
      number++; 
     } 

     System.out.println(""); 
     ++i; 
     for (int j = arraySize-1; j >= 0; --j) 

     { 
     pattern[i][j] = number; 
     System.out.printf("%3d", pattern[i][j]); 
     number++; 
     } 

     System.out.println(""); 
     ++i;  
    } 
    } 
} 

回答

0

我已經改變了你的代碼,以便它的作品。正如你已經提到的,你可以在這裏使用模運算符。

public static void main(String[] args) 
{ 

    Scanner keyboard = new Scanner(System.in); 

    System.out.println("[-------------------------]"); 
    System.out.println("[  Array Pattern  ]"); 
    System.out.println("[-------------------------]"); 

    System.out.println("How many rows/columns do you want your array to have? (Mist be at least 3):"); 


    int arraySize = keyboard.nextInt(); 

    while(arraySize < 3) 
    { 
     System.out.println("Lets try this again ...."); 
     System.out.println("How many rows/columns do you want your array to have? (Mist be at least 3):"); 
     arraySize = keyboard.nextInt(); 
    } 


    int [][] pattern = new int[arraySize][arraySize]; 

    int number = 1; 

    for(int i=0;i<arraySize;i++){ 
     if(i%2==0){ 
      for(int j=0;j<arraySize;j++){ 
       pattern[i][j]=number; 
       number++; 
      } 
     } 
     else{ 
      for(int j=arraySize-1;j>=0;j--){ 
       pattern[i][j]=number; 
       number++; 
      } 
     } 
     for(int j=0;j<arraySize;j++){ 
      System.out.printf("%3d", pattern[i][j]); 
     } 
     System.out.println(); 
    } 
} 
+0

好吧,我看到我的錯誤是當我用if/else。謝謝! – Mike

0

編輯:在IDE中對此進行測試以確保其正常工作。

您首先需要有模式[i] [j]的初始值。

for (int i = 0; i < arraySize; i++) { 
    for (int j = 0; j < arraySize; j++) { 
     pattern[i][j] = arraySize*i + j + 1; 
    } 
} 

你需要再有,將在升序打印出的數字時,我甚至是一個條件,當我是奇數下降。

for (int i = 0; i < arraySize; i++) { 
    if (i % 2 == 0) { 
     for (int j = 0; j < arraySize; j++) { 
      System.out.printf("%3d", pattern[i][j]); 
     } 
    } else { 
     for (int j = arraySize - 1; j >= 0; j--) { 
      System.out.printf("%3d", pattern[i][j]); 
     } 
    } 
    System.out.println(); 
}