2011-03-12 82 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板來簡化您的問題?

此時,您的代碼將會足夠小,以至於您可以一次一個遍歷每次迭代,並瞭解代碼的行爲與您所期望的不同。您可以按照代碼中的位置來添加更多的打印語句,或者使用調試程序遍歷代碼。