2 回答
TA贡献1789条经验 获得超10个赞
这里:
System.out.print("Enter the size of the array: ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int twoD[][] = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
twoD[i][j] = (int) (Math.random() * 2);
}
}
for (int[] x : twoD) {
for (int y : x) {
System.out.print(y + " ");
}
System.out.println();
}
输出:
Enter the size of the array: 4
1 0 0 0
1 0 0 1
0 1 0 0
1 0 1 1
TA贡献2003条经验 获得超2个赞
我假设你想这样做:
System.out.println("Please enter two-dimentional array size:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Random rand = new Random();;
int[][] array = new int[n][n];
for (int[] i : array) {
for (int j : i) {
j = rand.nextInt(2);
System.out.print(j + " ");
}
System.out.println("\n");
添加回答
举报