开发者

Need help to print scrabble board!

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();
    }*/

}

It should print out something like

    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  _  _  _  _  _  _  _  _  _  _  _  _  _  _  _

For some reason my code is not working help me to fix it plz


Why don't you simplify your problem by printing a 3x3 board first?

At that point, your code will be small enough that you can go through each iteration one at a time and understand where your code behaves differently than what you want. You can follow where you are in your code either my adding more print statements or stepping through your code with a debugger.


You can try this, I think it generates better output:

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]);
                    }
                }
            }
        }
    }
}


I think you need to break some of this into separate for loops so you've got a better handle on what's going on. Print your numbers across the top in a separate loop before you get into printing the whole grid. Renaming i and j to row and column might help you too.

Right now, you're iterating over i from 0 to 14, and printing i - 1, which would give you rows numbered -1 to 13. But then you're not printing anything when i == 0, which is why you're getting row numbers 0 to 13 but no 14. As you get further along you'll also realize you want the code that assigns the initial blanks to the board to be separate from the code that prints the board out, otherwise you'll be overwriting any letters that are added.


Update:

I think going < was the right approach, but this is the first change I'd make:

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]);
        }
    }
}

You can see that printing the column headers separately makes the main loop quite a bit simpler. I gave you a start on getting things aligned in the column header, you'll have to do something similar in the main loop to keep things lined up.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜