jeudi 21 mai 2020

Java combine an 2d array with an scanner and if-Statement?

I am new to this platform and trying to learn java. I hope that you guys could help me with my problem.

For example, if you have an array like:

int[][] gamefield= new int[4][4]

gamefield[0][0] = 1;

gamefield[0][1] = 2;

gamefield[0][2] = 3;

gamefield[0][3] = 4;

and so on...

Now you want a method called coordinate

like:

    static void coordinate(int[][] gamefield) {
    int row= 0;
    int column= 0;

    Scanner entry= new Scanner(System.in);

    System.out.println("\n\nEnter a number to move");

    System.out.print("row: ");
    row = entry.nextInt();

    System.out.print("column: ");
    column = entry.nextInt();
}

Ok all good for now, but I want to replace, for example an element with an if-statement or something else, how can i do this?


if (entry.equals(gamefield[0][0])) {
            gamefield[0][0] = 2;
        } */ ```

// So if the user types for the row 0 and for the column 0, the element in the array in the position [0][0] should be 2 and not 1.

I hope you guys can help me, i would really appreciate it! If something is unclear, please ask me, i hope i could explain my problem.

Aucun commentaire:

Enregistrer un commentaire