I’m trying to print a 2D array of random numbers from 1 to 15 only once. I’ve been able to print out the array but only sequentially.
int x =0;
public void Numberbox(){
int[][] a2 = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,x}};
String output = ""; // Accumulate text here (should be StringBuilder).
//... Print array in rectangular form using nested for loops.
for (int row = 0; row < ROWS; row++)
{
for (int col = 0; col < COLS; col++)
{
output += " " + a2[row][col];
}
output += "\n";
}
System.out.print(output);
}
You could use a collection, for example a list, and use the built-in shuffle function. For example: