Question 20 (10 points)
Write the Java code required to solve the following problem. A complete program is
required and must follow all programming conventions (indenting, variable names,
comments, etc...).
You are to create a program which will place 8 queens on a chess board.
1 - Create a blank chessboard (8 x8 2D array of integers, all O's)
2 - Using a loop, ask the user for 8 locations on the board, replace the O with a 1 at.
this location.
3 - Display the final board.
You do not need to include any error checking or checking for duplicate locations.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int[][] board = new int[8][8];
Scanner in = new Scanner(System.in);
for (int i = 0; i < 8; i++) {
System.out.println("Row : Col");
board[in.nextInt()][in.nextInt()] = 1;
}
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
System.out.print(board[i][j]+" ");
}
System.out.println();
}
}
}
Comments
Leave a comment