2016-04-25 86 views
0

這個問題涉及我的二維數組顯示,目前看起來像這樣。二維數組顯示

A B C D 
1: 0 0 0 0 
2: 0 0 0 0 
3: 0 0 0 0 

我試圖讓位置(0,0)更改爲數字1,因爲這將是我的計數的開始。

然而,它不會改變,並保持爲零,這是我的代碼。

int[][] chessBoard = new int[3][4];                     
int rowhead = 1;                          
    TextIO.put(" ");                        
for (int col = 0; col < chessBoard[0].length; col++)                 
    TextIO.putf("%4s",((char) ('A' + col)));                   
    TextIO.putln();                         

for (int [] row:chessBoard){                       
    TextIO.put(rowhead++ + ":");                      
for (int griddisplay:row)                      
    TextIO.putf("%4d", griddisplay);                     
    TextIO.putln();                         
    chessBoard [0][0] = 1; 

現在,這使我的座標(0,0-)顯示爲零,但是如果我改變這個 棋盤[0] [0] = 1; 至此 chessBoard [1] [0] = 1;

那麼,網格也相應變爲

A B C D 
1: 0 0 0 0 
2: 1 0 0 0 
3: 0 0 0 0 

我要去哪裏錯了?

+0

似乎你要更改之前打印? –

+1

什麼是TextIO?此代碼不完整,並且縮進表示您可能不理解for循環的範圍。 –

+0

你可以發佈更多的代碼嗎? – sebenalern

回答

0

移動你chessBoard [0][0] = 1;正下方TextIO.put(rowhead++ + ":");

int[][] chessBoard = new int[3][4]; 
    int rowhead = 1; 
    TextIO.put(" "); 
    for (int col = 0; col < chessBoard[0].length; col++) 
     TextIO.putf("%4s",((char) ('A' + col))); 
    TextIO.putln(); 

    for (int [] row:chessBoard) { 
     TextIO.put(rowhead++ + ":"); 
     chessBoard[2][3] = 1; 
     for (int griddisplay : row){ 
      TextIO.putf("%4d", griddisplay); 
     } 
     TextIO.putln(); 
    } 

,它會propertly =工作)。

+0

我測試了代碼我的自我,它的工作原理,我不明白爲什麼downvote? – Leo

+0

我按照你的建議完成了,你是正確的,因爲它確實有效。我對此很新,很不確定你爲什麼被打倒! – JavaTheHutt

+0

如果能解決您的問題,請您接受答案嗎? – Leo

-1

你似乎有一些格式問題,但是從它的要點:

您正在建設的國際象棋棋盤爲數組[3] [4]和它的外觀是3「行」和4'列',所以你的第一個索引是你的行號,第二個是列號。

你通過第一個'行'循環獲得長度(4),所以輸出4列 - 正確地我假設。

然後您將移動到打印棋盤(儘管我沒有看到匹配的花括號})。首先你循環行,然後跨列。

例如:

for (int [] row:chessBoard) {                       
    TextIO.put(rowhead++ + ":");                      
    for (int griddisplay:row)                      
     TextIO.putf("%4d", griddisplay);                     
    TextIO.putln();                        
    chessBoard [0][0] = 1; 
} 

如果這是你的代碼,第一個循環開始處理的第一行。它循環遍歷第一行的列,然後將[0] [0]設置爲1 ...但是您已經打印了所以它不會顯示。如果用[1] [0]替換它,它實際上會設置在打印第二行之前如此正確地顯示該值。

作爲最後一個提示,大括號指定for循環的範圍。如果省略大括號,則循環僅在其後面運行語句。很多程序員習慣的習慣是始終明確地使用大括號來避免容易犯的錯誤。

1

你的代碼工作正常,我只是增加了一些方法和改變輸出

public class Chessboard 
{ 

    public static void main(String[] args) 
    { 
     int[][] chessBoard = new int[3][4];                                              

     print(chessBoard); 

     chessBoard [0][0] = 1; 
     print(chessBoard); 

     chessBoard [1][0] = 1; 
     print(chessBoard); 

     clear(chessBoard); 
     print(chessBoard); 
    } 

    public static void print(int[][] chessBoard) 
    { 
     int rowhead = 1; 
     System.out.print("\n ");                        
     for (int col = 0; col < chessBoard[0].length; col++) 
      System.out.printf("%4s",((char) ('A' + col)));                  
     System.out.println();                         

     for (int[] row : chessBoard) 
     { 
      System.out.print(rowhead++ + ":");                      
      for (int griddisplay : row)                      
       System.out.printf("%4d", griddisplay);                     
      System.out.println();                         
     } 
     System.out.println(); 
    } 

    public static void clear(int[][] chessBoard) 
    { 
     for (int row = 0; row < chessBoard.length; row++) 
      for(int col = 0; col < chessBoard[row].length; col++) 
       chessBoard[row][col] = 0; 
    } 

}