How do I make a variable to count the number of tiles through the program

Viewed 99

I am basically making this java program about a tile game for the user to interact with. This is one of the 6 classes and I need a variable to keep track of the tiles. How do I do that?

/*
 * Activity 8.2 Tester File
 */
public class TileTester {
    private static String[] tileValues = {
            "lion", "lion",
            "penguin", "penguin",
            "dolphin", "dolphin",
            "fox", "fox",
            "monkey", "monkey",
            "turtle", "turtle"};

    public static void main(String[] args) {
        /*2d array*/
        String[][] gameBoard = {{"lion", "lion", "penguin", "penguin"},
                {"dolphin", "dolphin", "fox", "fox"},
                {"monkey", "monkey", "turtle", "turtle"}};
        /*nested for loop*/
        for (int row = 0; row < gameBoard.length; row++) {
            for (int column = 0; column < gameBoard[0].length; column++) {
                System.out.println(gameBoard[row][column]);
            }
        }
    }
}
1 Answers

I don't get your question fully, but in second loop you should use:

gameBoard[row].length;

for(int column = 0; column < gameBoard[row].length; column++) {
    System.out.println(gameBoard[row][column]);  
}
Related