How to print a 2d char array into a 5x5 game board and intialize it to store "O's" in JAVA -
i creating 5x5 battleship game board. used nested loop print board it's not printing correctly 5x5 board. can see went wrong in "for loop"? instructions
- build 5x5 char array(game board)
- initialize board store "o's"
- print board
this output:
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
this code have far.
public class location { int row; int col; // constructor, gettes & setters public static void main(string[] args) { //creates array char[][] grid = new char [5][5]; //prints array (int row=0; row<grid.length; row++) { (int col=0; col<grid[row].length; col++) system.out.print(grid[row][col]); system.out.println(); } } } }
like @pshemo said in comments, need initialize grid.
//creates array char[][] grid = new char [5][5]; //initialize array for(int row=0; row<grid.length; row++) { for(int col=0; col<grid[row].length; col++) grid[row][col] = 'o'; }
Comments
Post a Comment