samedi 2 février 2019

How do I generate 2d array table with non-repeating numbers?

How should I generate a 4X4 2D array table with every element different?

Here's my code:

public class Game {
public static void main(String[] args) {
    int gameboard[][] = new int[4][4];
    for (int row=0; row < gameboard.length; row++) {
        for (int col=0; col < gameboard[row].length; col++) {
            gameboard[row][col] = ((int)(1+Math.random() * 16));
            System.out.printf("%-4d",gameboard[row][col]);
        }
        System.out.println();
        }
      }

}




Aucun commentaire:

Enregistrer un commentaire