2011-03-12 78 views
2
public class Project1 { 
    public static void main(String [] args) { 
     //ScrabbleLetterBag letterBag = new ScrabbleLetterBag(); 
     char [][] scrabbleBoard = new char [15][15]; 

     for (int i = 0; i <=scrabbleBoard.length; i++) { 
      if (i != 0) { 
       System.out.println(" "); 
       System.out.print(i - 1); 
      } 

      for (int j = 0; j <=scrabbleBoard.length; j++) { 
       if (j == 8 && i == 8) { 
        scrabbleBoard[i][j] = '*'; 
        System.out.print(scrabbleBoard[i][j]); 
       } 
       else { 
        if (i == 0) { 
         System.out.print(" "+j); 
        } 
        else{ 
         scrabbleBoard[i][j] = '_'; 
         System.out.print(" "); 
         System.out.print(scrabbleBoard[i][j]); 
        } 
       } 
      } 
     } 
    } 


    /*int count = 0; 
    char myLetter; 
    while (!letterBag.isEmpty()) { 
     count++; 
     myLetter = letterBag.getLetter(); 
     System.out.print(myLetter); 
     if (count % 10 == 0) 
      System.out.println(); 
    }*/ 

} 

应该打印出类似需要帮助打印拼字板!

 
    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 

0 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
1 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
2 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
3 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
4 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
5 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
6 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
7 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
8 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
9 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
10 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
11 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
12 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
13 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
14 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

出于某种原因,我的代码是不工作的帮助我解决它PLZ

+0

什么是不工作?它是否编译?有什么打印? – 2011-03-12 04:47:14

+0

你不会在你的预期输出中的任何地方显示'*',但它在你的代码中。 – 2011-03-12 04:48:24

+0

这不是打印行14 – ZZZ 2011-03-12 04:49:51

回答

0

你可以试试这个,我认为它会产生更好的输出:

public class Project1 { 
    public static void main(String [] args) { 
     //ScrabbleLetterBag letterBag = new ScrabbleLetterBag(); 
     char [][] scrabbleBoard = new char [16][16]; 

     for (int i = 0; i<scrabbleBoard.length; i++) { 
      if (i != 0) { 
       System.out.println("\t"); 
       System.out.print(i-1); 
      } 

      for (int j = 1; j <scrabbleBoard.length; j++) { 
       if (j == 8 && i == 8) { 
        scrabbleBoard[i][j] = '*'; 
        System.out.print(scrabbleBoard[i][j]); 
       } 
       else { 
        if (i == 0) { 
         System.out.print("\t"); 
         System.out.print(j-1); 
        } 
        else { 
         scrabbleBoard[i][j] = '_'; 
         System.out.print("\t"); 
         System.out.print(""+scrabbleBoard[i][j]); 
        } 
       } 
      } 
     } 
    } 
} 
0

我认为你需要打破一些这方面为独立的for循环使你已经更好地掌握了正在发生的事情。在打印整个网格之前,在一个单独的循环中在顶部打印数字。重命名为ijrowcolumn也可能对您有所帮助。

现在,你遍历i014,并打印i - 1,这将使你编号-113行。但是,当i == 0时,你不打印任何东西,这就是为什么你得到行号013但没有14。随着你的进一步发展,你也会意识到你需要的代码将初始空白分配给开发板,以便与打印开发板的代码分开,否则你将覆盖所有添加的字母。


更新:

我觉得要去<是正确的方法,但是这是第一个变化我会做:

System.out.print(" "); // indent so there's room for row numbers 
for (int col = 0; col < scrabbleBoard.length; col++) { 
    // give single digit numbers extra padding to keep them aligned 
    if (col < 10) 
     System.out.print(" " + col); 
    else 
     System.out.print(" " + col); 
} 
System.out.println(); 

for (int i = 0; i < scrabbleBoard.length; i++) { 
    System.out.print(i - 1); 

    for (int j = 0; j < scrabbleBoard.length; j++) { 
     if (j == 8 && i == 8) { 
      scrabbleBoard[i][j] = '*'; 
      System.out.print(scrabbleBoard[i][j]); 
     } 
     else { 
      scrabbleBoard[i][j] = '_'; 
      System.out.print(" "); 
      System.out.print(scrabbleBoard[i][j]); 
     } 
    } 
} 

你可以看到打印列标题分别做主循环比较简单一些。我给你开始在列标题中对齐,你必须在主循环中做类似的事情来保持事物的排列。

+0

我编辑了我的程序,现在两个for循环都是<=,因为我希望第一行和第一行是从0开始的数字计数,但它不工作。 – ZZZ 2011-03-12 05:17:19

+0

非常感谢 – ZZZ 2011-03-12 21:57:22

1

为什么不通过先打印3x3板来简化您的问题?

此时,您的代码将会足够小,以至于您可以一次一个遍历每次迭代,并了解代码的行为与您所期望的不同。您可以按照代码中的位置来添加更多的打印语句,或者使用调试程序遍历代码。