I have to write a JAVA
program where a user set the number of columns and rows of a 2d
array. Then he should choose a minimum and a maximum. After that, the array is filled randomly.
I writed this code:
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class array2d
{
public static void main (String[] args) throws java.lang.Exception
{
int rows, col, min, max;
Scanner scan1 = new Scanner(System.in);
System.out.println("Enter number of rows and columns:");
rows = scan1.nextInt();
col = scan1.nextInt();
int[][] a = new int[rows][col];
System.out.println("Enter min and max:");
min = scan1.nextInt();
max = scan1.nextInt();
for(int i = 0; i<rows; i++)
{
for(int j = 0; j<col; j++)
{
a[i][j] = min - (int)Math.random()*(max-min+1);
}
}
//Display on the screen
for(int i = 0; i<rows; i++)
{
for(int j = 0; j<col; j++)
{
System.out.print(a[i][j]+ " ");
}
}
...
}
}
Then we should run a program to see if the first number of each row is a divisor of all the row:
for(int i = 0; i<rows; i++)
{
for(int j = 0; j<col; j++)
{
if(a[i][0]%a[i][j]==0)
{
System.out.println(a[i][j]);
}
else
System.out.println("None");
}
}
That's working properly, but the generated array on my computer is displayed like that (CMD output):
And as you see in the picture, all randomly filled random are equal to the minimum specified.
So how can I display this array as like matrix and why the random numbers are showing like this.