用java 编写一个矩阵类Matrix,要求能利用该矩阵类生成任意行、列的矩阵对象,比如3╳5矩阵,10╳20矩阵,并用[0-20]之间的随机整数来初始化矩阵中的各个数据,并且写出测试代码来证明你写的类可以正确运行。
public class Matrix {
private int [] [] array = null;
public Matrix () {
}
public Matrix (int row, int col){
array = new int [row][col];
}
public void showMatrix(){
for(int i = 0 ; i < array.length; i++){
for(int j = 0; j < array.length; j++){
System.out.print(array[i][j]+" ");
}
System.out.println();
}
}
public static void main(String[] arge) {
Matrix m1 = new Matrix(3,5);
m1.showMatrix();
Matrix m2 = new Matrix(10,20);
m2.showMatrix();
}
}这个这么编写 并用[0-20]之间的随机整数来初始化矩阵中的各个数据,
用java 编写一个矩阵类Matrix,要求能利用该矩阵类生成任意行、列的矩阵对象,比如3╳5矩阵,
答案:2 悬赏:30
解决时间 2021-02-04 18:37
- 提问者网友:枫涩帘淞幕雨
- 2021-02-04 05:18
最佳答案
- 二级知识专家网友:时光不老我们不分离
- 2021-02-04 05:48
public class Matrix {
private int row=0;
private int col=0;
private int [] [] array = null;
public Matrix () {}
public Matrix (int row, int col){
this.row=row;
this.col=col;
array = new int [row][col];
}
public void showMatrix(){
for(int i = 0 ; i < row; i++){
for(int j = 0; j < col; j++){
array[i][j]=(int)(Math.random()*20)+1;
System.out.print(array[i][j]+"\t");
}
System.out.println();
}
}
public static void main(String[] arge) {
Matrix m1 = new Matrix(3,5);
m1.showMatrix();
System.out.println;
Matrix m2 = new Matrix(10,20);
m2.showMatrix();
}
}
private int row=0;
private int col=0;
private int [] [] array = null;
public Matrix () {}
public Matrix (int row, int col){
this.row=row;
this.col=col;
array = new int [row][col];
}
public void showMatrix(){
for(int i = 0 ; i < row; i++){
for(int j = 0; j < col; j++){
array[i][j]=(int)(Math.random()*20)+1;
System.out.print(array[i][j]+"\t");
}
System.out.println();
}
}
public static void main(String[] arge) {
Matrix m1 = new Matrix(3,5);
m1.showMatrix();
System.out.println;
Matrix m2 = new Matrix(10,20);
m2.showMatrix();
}
}
全部回答
- 1楼网友:ー何必说爱
- 2021-02-04 06:34
public class matrix {
// 表示行和列
private int mrow, mcolumn;
// 构造方法
public matrix(int row, int column) {
mrow = row;
mcolumn = column;
}
// 获取0-20随机数
private int random() {
double random1 = math.random();// 这个math类的方法可以获取0.0-1.0之间的随机数
double random2 = random1 * 20; // 0.0 - 20.0
return (int) random2;
}
// 创建矩阵
private void creatematrix() {
int totalcount = mrow * mcolumn; // 总共有那么多
for (int count = 1; count <= totalcount; count++) {
int number = random();// 上面的方法
system.out.print(number < 10 ? "0" + number : number); // 输出数字,如果数字小于10,前面加0补全两位
system.out.print(" "); // 分隔符,随便填
if (count % mrow == 0) {
system.out.println(); // 换行
}
}
}
public static void main(string[] args) {
matrix matrix = new matrix(3, 5); // 几行几列传进去
matrix.creatematrix();
}
}为了经验我也是蛮拼的了
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯