I need help with a java program I need a program that will t

I need help with a java program, I need a program that will take in x and y coordinates of a 10 by 10 board from the user. Must use 2D array. Think of it like the game battleship
I need help with a java program, I need a program that will take in x and y coordinates of a 10 by 10 board from the user. Must use 2D array. Think of it like the game battleship

Solution

Please follow the code and commenbts for description :

CODE :

import java.util.Scanner;

public class Array2D { // class to run the code

public static void main(String[] args) { // driver method
  
Scanner sc = new Scanner(System.in); // scanner class to get the data from the user
int[][] board = new int[10][10]; // initialisaing a board with the desired size
System.out.println(\"Initial Board.\"); // prompt for the user
System.out.println(\"\\t1 \\t2 \\t3 \\t4 \\t5 \\t6 \\t7 \\t8 \\t9 \\t10\");
System.out.println();
for (int i = 0; i < 10; i++) { // printing the initial board
System.out.print((i + 1) + \"\");
for (int j = 0; j < 10; j++) {
System.out.print(\"\\t\" + \"~\");
}
System.out.println();
}
  
System.out.println(\"\ Enter the X Coordinate : \"); // get the dat from the user
int row = sc.nextInt();
System.out.println(\"\ Enter the Y Coordinate : \");
int column = sc.nextInt();
  
System.out.println(\"Updated Board.\"); // print the updated board
System.out.println(\"\\t1 \\t2 \\t3 \\t4 \\t5 \\t6 \\t7 \\t8 \\t9 \\t10\");
System.out.println();
for (int i = 0; i < 10; i++) {
System.out.print((i + 1) + \"\");
for (int j = 0; j < 10; j++) {
if((i + 1 == row) && (j + 1 == column)){
System.out.print(\"\\t\" + \"X\");
} else {
System.out.print(\"\\t\" + \"~\");
}
  
}
System.out.println();
}
}
}


OUTPUT :

Initial Board.
   1    2    3    4    5    6    7    8    9    10

1   ~   ~   ~   ~   ~   ~   ~   ~   ~   ~
2   ~   ~   ~   ~   ~   ~   ~   ~   ~   ~
3   ~   ~   ~   ~   ~   ~   ~   ~   ~   ~
4   ~   ~   ~   ~   ~   ~   ~   ~   ~   ~
5   ~   ~   ~   ~   ~   ~   ~   ~   ~   ~
6   ~   ~   ~   ~   ~   ~   ~   ~   ~   ~
7   ~   ~   ~   ~   ~   ~   ~   ~   ~   ~
8   ~   ~   ~   ~   ~   ~   ~   ~   ~   ~
9   ~   ~   ~   ~   ~   ~   ~   ~   ~   ~
10   ~   ~   ~   ~   ~   ~   ~   ~   ~   ~

Enter the X Coordinate :
7

Enter the Y Coordinate :
7
Updated Board.
   1    2    3    4    5    6    7    8    9    10

1   ~   ~   ~   ~   ~   ~   ~   ~   ~   ~
2   ~   ~   ~   ~   ~   ~   ~   ~   ~   ~
3   ~   ~   ~   ~   ~   ~   ~   ~   ~   ~
4   ~   ~   ~   ~   ~   ~   ~   ~   ~   ~
5   ~   ~   ~   ~   ~   ~   ~   ~   ~   ~
6   ~   ~   ~   ~   ~   ~   ~   ~   ~   ~
7   ~   ~   ~   ~   ~   ~   X   ~   ~   ~
8   ~   ~   ~   ~   ~   ~   ~   ~   ~   ~
9   ~   ~   ~   ~   ~   ~   ~   ~   ~   ~
10   ~   ~   ~   ~   ~   ~   ~   ~   ~   ~

Hope this is helpful.

I need help with a java program, I need a program that will take in x and y coordinates of a 10 by 10 board from the user. Must use 2D array. Think of it like t
I need help with a java program, I need a program that will take in x and y coordinates of a 10 by 10 board from the user. Must use 2D array. Think of it like t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site