2016-04-25 80 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; 
    } 

}