简体   繁体   中英

How to insert a value in 2D array in java

How can I fill a 3x3 matrix using a 2D array such that the user picks what position of the array they want to input their String value?

The position format is: (Row Number, Column Number)

For example:

Person 1, please pick where to place an X: (0,1)
Person 2, please pick where to place an O: (0,2)

This is what I have:

import java.util.Scanner;

public class idk {
    public static void main(String[] args)

    {
        int i;
        int j;
        int arr[][] = new int[3][3];

        // Getting user input
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a number: ");
        for (i = 0; i < 3; i++) {
            for (j = 0; j < 3; j++) {
                arr[i][j] = input.nextInt();
             }

        }

        // Outputting the user input
        System.out.println("The output is: ");
        for (i = 0; i < 3; i++) {
            for (j = 0; j < 3; j++) {
                System.out.printf("%d ", arr[i][j]);
            }
        }
    }    
}

Simply

arr[Row Number][Column Number]=X;

eg

arr[0][1]=X;

or

arr[1][0]=O;

but because it is an int array you cannot place String ie "O" and "X" in it.

Try making it an String array

Something like the following has most of the parts you would want, except for error handling. Input is number, space (or any whitespace), finally followed by another number. Input numbers are 1 to 3 inclusive, which is what a normal person would expect.

import java.util.Scanner;

public class TicTacToe {

    char board[][] = new char[][]{{'-','-','-'},{'-','-','-'},{'-','-','-'}};

    public static void main(String[] args) {

        TicTacToe ttt = new TicTacToe();
        ttt.run();
    }

    public void run() {
        Scanner input  = new Scanner(System.in);
        int row = -1, col = -1;//just to initialize 
        char symbol = 'o';

        while (true) {
            symbol = (symbol == 'x')?'o':'x';
            boolean error = false;

            System.out.println("Enter a number: ");
            if (input.hasNext()) {
                row = input.nextInt();
                System.out.println("row: " + row);
            } else {
                error = true;
            }
            if (input.hasNext()) {
                col = input.nextInt();
                System.out.println("col: " + col);
            } else {
                error = true;
            }
            if (!error) {
                board[row - 1][col - 1] = symbol;
            }else{
                System.out.println("an error has occurred");
            }
            input.reset();
            this.drawBoard();
        }
    }

    public void drawBoard() {
        System.out.println("The output is: ");
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.printf("%c ", board[i][j]);
            }
            System.out.println();
        }
        System.out.println();
    }
}

If you look up Scanner you will see an example to parse using a regex, I like that method better since with a regex you can validate the whole string at once but since that was not in the questions code I didn't use that method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM