Answer to Question #301302 in Java | JSP | JSF for Same

Question #301302

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 (8x 8 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.


1
Expert's answer
2022-02-22T13:42:55-0500
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();
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS