public class MatrixZeroOne {
public static void main(String[] args) {
int randomNumber;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
randomNumber = (int)(Math.random() * 2);
printMatrix(randomNumber);
}
System.out.println();
}
}
public static void printMatrix(int n) {
System.out.print(n + " ");
}
}
/*
0 1 0
1 0 0
0 1 0
1 1 0
0 0 0
1 0 0
0 0 1
0 0 1
1 0 1
*/
Introduction to Java Programming编程题5.17<01矩阵>
生成随机01矩阵
本文介绍了一个简单的Java程序,该程序用于生成3x3的随机01矩阵,并演示了如何使用基本的循环和数学函数来实现这一功能。此外,还提供了一个printMatrix方法来展示生成的矩阵。

被折叠的 条评论
为什么被折叠?



