中易网

用java 编写一个矩阵类Matrix,要求能利用该矩阵类生成任意行、列的矩阵对象,比如3╳5矩阵,

答案:2  悬赏:30  
解决时间 2021-02-04 18:37
用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]之间的随机整数来初始化矩阵中的各个数据,
最佳答案
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();
}
}
全部回答
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(); } }为了经验我也是蛮拼的了
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
下面图片里的动漫人物是谁?
怎样把ANSYS中划分好网格的模型导入Fluent中
漫威616宇宙和终极宇宙毁灭在第几话
海信专卖店南黄店地址有知道的么?有点事想过
镇江市规划局京口分局我想知道这个在什么地方
我想知道从威海到上海坐飞机要几个小时
怎样学习人教版高中数学必修3算法.统计.概率
蔚县农民合作经济组织联合会在哪里啊,我有事
雅居万家装饰怎么去啊,有知道地址的么
完美漫画怎么使用
求静液压系统回路装配图,其中液压泵选择力士
你是在哪里学的变速箱修理的
1979年阴历1月初3什么星座
林之森信息咨询策划中心怎么去啊,有知道地址
影楼照片后期处理软件 影楼用什么软件修照片
推荐资讯
鹏旺腊汁肉白吉馍阜阳店地址有知道的么?有点
魅族p5和p6哪个好
lasun是什么布线品牌?
脚拇指筋断一根已接好,算几级伤残。怎样办理
伦敦大雾灾当时的情况是怎样?为什么会这么严
请问下这种红壳芙蓉王哪里有卖?是出口的吗?
高密市井沟镇前下口村村民委员会地址在哪,我
EXCEL表格中,提出关键数据,直接形成日报、
建行最多能存多少钱
在房屋拆迁过程中,被拆迁方与拆迁方在规定拆
乐易购超市在什么地方啊,我要过去处理事情
求-商业出路,创意点子
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?