2011-10-16 205 views
16

我有一個[20] [20]我已經操作的二維數組。簡單地說,我正在做一個用戶輸入指令的海龜項目,例如pen up = 0和pen down = 1.當筆落在單獨的陣列位置時,例如[3] [4]被標記爲「1」 。Java - 打印二維數組

我的程序的最後一步是打印出20/20陣列。

我無法弄清楚如何打印它,我需要用一個「X」

打印命令實際上是一個類,父程序將調用裏面的方法來代替「1」 .. 。我知道我必須使用循環...任何幫助,將不勝感激。

public void printGrid() 
     { 
      System.out.println... 
     } 

回答

24
public void printGrid() 
{ 
    for(int i = 0; i < 20; i++) 
    { 
     for(int j = 0; j < 20; j++) 
     { 
     System.out.printf("%5d ", a[i][j]); 
     } 
     System.out.println(); 
    } 
} 

而更換

public void replaceGrid() 
{ 
    for (int i = 0; i < 20; i++) 
    { 
     for (int j = 0; j < 20; j++) 
     { 
     if (a[i][j] == 1) 
      a[i][j] = x; 
     } 
    } 
} 

你可以做到這一切一氣呵成:

public void printAndReplaceGrid() 
{ 
    for(int i = 0; i < 20; i++) 
    { 
     for(int j = 0; j < 20; j++) 
     { 
     if (a[i][j] == 1) 
      a[i][j] = x; 
     System.out.printf("%5d ", a[i][j]); 
     } 
     System.out.println(); 
    } 
} 
+0

嗎? – user997462

+0

當然!在' – COD3BOY

+0

裏面加'if'參見更新...... – COD3BOY

1

你應該通過環行,然後列一個結構類似

for ...row index... 
    for ...column index... 
    print 

,但我想這是功課所以只是嘗試一下自己。

在for循環中交換行/列索引,具體取決於是否需要先遍歷然後再遍歷,然後再遍歷。

+0

我該如何用x替換1?順便說一句,不要做作業...我是一個40歲的IT專業人員(網絡),他總是對編程感興趣,因此學習了一些新東西。 – user997462

+0

只要檢查旅途中的字符(在循環中),並將它替換爲'1' – Jack

+0

@Jack - 像'print(a [i] [j] == 1?'X':'');我可以在printGrid方法中替換內部的' –

18

事情是這樣的,我在另一個問題

public class Snippet { 
    public static void main(String[] args) { 
     int [][]lst = new int[10][10]; 

     for (int[] arr : lst) { 
      System.out.println(Arrays.toString(arr)); 
     } 
    } 

} 
1

回答那麼,因爲'X'是一個char而不是int,所以你不能採取行動在矩陣本身ually取代它,但是,下面的代碼應該打印一個「X」字符時,它遇到一個1

public void printGrid(int[][] in){ 
    for(int i = 0; i < 20; i++){ 
     for(int j = 0; j < 20; j++){ 
      if(in[i][j] == 1) 
       System.out.print('X' + "\t"); 
      else 
       System.out.print(in[i][j] + "\t"); 
     } 
     System.out.print("\n"); 
    } 
} 
+0

如何格式化這個以打印一個20乘20的網格? – user997462

+0

這應該打印得很好。您可以在每個打印語句的末尾添加製表符(「\ t」或任何字符,您的首選項)以分隔每個元素。 – Wyatt915

27

可以使用工具mettod。 Arrays.deeptoString();

public static void main(String[] args) { 
    int twoD[][] = new int[4][]; 
    twoD[0] = new int[1]; 
    twoD[1] = new int[2]; 
    twoD[2] = new int[3]; 
    twoD[3] = new int[4]; 

    System.out.println(Arrays.deepToString(twoD)); 

} 
+1

'數組。deepToString'只打印二維數組,但只在一行上,儘可能有用,它不是問題所在(網格)。 –

0

如果你知道MAXVALUE來矩陣的,我發現下面的代碼更有效和通用的(可以很容易地,如果元素的另一次迭代是不是一個問題做)。

int numDigits = (int) Math.log10(maxValue) + 1; 
    if (numDigits <= 1) { 
     numDigits = 2; 
    } 
    StringBuffer buf = new StringBuffer(); 
    for (int i = 0; i < matrix.length; i++) { 
     int[] row = matrix[i]; 
     for (int j = 0; j < row.length; j++) { 
      int block = row[j]; 
      buf.append(String.format("%" + numDigits + "d", block)); 
      if (j >= row.length - 1) { 
       buf.append("\n"); 
      } 
     } 
    } 
    return buf.toString(); 
1

試試這個怎麼樣?

public static void main (String [] args) 
{ 
    int [] [] listTwo = new int [5][5]; 
    // 2 Dimensional array 
    int x = 0; 
    int y = 0; 
    while (x < 5) { 
     listTwo[x][y] = (int)(Math.random()*10); 

     while (y <5){ 
      listTwo [x] [y] = (int)(Math.random()*10); 
      System.out.print(listTwo[x][y]+" | "); 
      y++;     
     } 
     System.out.println(""); 
     y=0; 
     x++; 
    } 
}