Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Allow 2 Users One at a Time to Place Their Ships on a Board

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 550
    Answer it

    Please check the code below and provide a solution for this:

     

    package Gui;
    import java.util.Scanner;


    public class Board {

        public static void main(String[] args) {
            // TODO Auto-generated method stub

            // Implement scanner5
            Scanner input = new Scanner(System.in);

            System.out.println("player1 please enter your name");
            String player1 = input.nextLine();
            
            System.out.println("player2 please enter your name");
            String player2 = input.nextLine();
            
            
            // create loop for accepting matrix input
            // first accept row size
            System.out.println("Please enter the number of rows in your matrix. Must be 35 or less.");
            int row = input.nextInt();
            

            while (row > 35 || row < 1) {
                System.out.println("Sorry. Your number is not the correct size. "
                        + "Please enter the number of rows in your matrix. Must be between 35 and 1.");
                row = input.nextInt();
            }
            
            // next accept column size
            System.out.println("Please enter the number of columns in your matrix. Must be 35 or less.");
            int column = input.nextInt();
            
            while (column > 35 || column < 1) {
                System.out.println("Sorry. Your number is not the correct size. "
                        + "Please enter the number of columns in your matrix. Must be between 35 and 1.");
                column = input.nextInt();
            }
            
            
            // declare array with row and columns the user gave
            int[][] userArray = new int[row][column];

            for(int i=0;i< row ; i++){
                for(int j=0; j< column; j++) {
                    System.out.print("");
                    int val = 0;
                        if(val>10 || val< -10) {
                            System.out.println("Invalid value.");
                            continue;
                        }
                    userArray[i][j] = val;
                }
            }
            printMatrix(userArray, row, column);
        }

        public static void printMatrix(int[][] array, int row, int column) {    
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < column; j++) {
                    System.out.print(array[i][j] + " ");
                    }
                    System.out.println();}
                    }
                }

 0 Answer(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: