Hello, I am trying to code a basic battleship game and need help with nested loops. I am trying to use stored values in first loop in the second loop to put an * instead of an X for the last row and column number.

This is my code so far...

import java.util.Random;

Code :

public class createBoard { public static void main(String args[]) { int squareRow = 0; // declares variable squareRow, location of row coordinate int squareColumn = 0; // declares variable squareColumn, location of column coordinate int direction = 0; // declares variable direction, direction of ship 1=north, 2=east, 3=south, 4=west int length = 0; // declares variable length, length of each ship String directionName = ""; // declares string variable directionName Random generator = new Random(); // declares new random variable generator for(int i = 0; i < 5; i++) // for loop generates random number for variables: loops 5 times { squareRow = generator.nextInt(10) + 1; // generates random number between 1 and 10 for row squareColumn = generator.nextInt(10) + 1; // generates random number between 1 and 10 for column direction = generator.nextInt(4) + 1; // generates random number between 1 and 4 for direction length = generator.nextInt(5) + 1; // generates random number between 1 and 5 for length System.out.println("Row: " + squareRow + ", Column: " + squareColumn + ", Direction: " + directionName + ", Length: " + length); if(direction == 1) directionName = "North"; // sets direction if 1 to north if(direction == 2) directionName = "East"; // sets direction if 2 to east if(direction == 3) directionName = "South"; // sets direction if 3 to south if(direction == 4) directionName = "West"; // sets direction if 4 to west } System.out.println(); System.out.println(" ABCDEFGHIJ"); int a = 0; for(int x = 1; x <= 10; x++) { a++; if(x == 1 || x == 2 || x == 3 || x == 4 || x == 5 || x == 6 || x == 7 || x == 8 || x == 8 || x == 9) System.out.print(a + " "); if(x == 10) System.out.print(a + " "); for(int y = 1; y <= 10; y++) { System.out.print("X"); } System.out.println(); } } }

This is the output....

Row: 1, Column: 9, Direction: South, Length: 4

Row: 10, Column: 5, Direction: West, Length: 4

Row: 6, Column: 9, Direction: West, Length: 1

Row: 1, Column: 1, Direction: West, Length: 3

Row: 3, Column: 3, Direction: East, Length: 2

ABCDEFGHIJ

1 XXXXXXXXXX

2 XXXXXXXXXX

3 XXXXXXXXXX

4 XXXXXXXXXX

5 XXXXXXXXXX

6 XXXXXXXXXX

7 XXXXXXXXXX

8 XXXXXXXXXX

9 XXXXXXXXXX

10 XXXXXXXXXX

This is how the out put should look. There should be an * corresponding with the row and column numbers.

Row: 1, Column: 9, Direction: South, Length: 4

Row: 10, Column: 5, Direction: West, Length: 4

Row: 6, Column: 9, Direction: West, Length: 1

Row: 1, Column: 1, Direction: West, Length: 3

Row: 3, Column: 3, Direction: East, Length: 2

ABCDEFGHIJ

1 *XXXXXXX*X

2 XXXXXXXXXX

3 XX*XXXXXXX

4 XXXXXXXXXX

5 XXXXXXXXXX

6 XXXXXXXX*X

7 XXXXXXXXXX

8 XXXXXXXXXX

9 XXXXXXXXXX

10 XXXX*XXXXX