没有输出的代码二数组乘法

7-3 使用二维数组实现Matrix(矩阵)。

分数 12

全屏浏览题目

切换布局

作者 tr

单位 成都信息工程大学

使用二维数组实现Matrix(矩阵)。

1. 定义Matrix(矩阵)类,要求如下:   
   a) 变量:matrix(int型二维数组),row(行数),column(列数);   
   b) 方法:实现两个矩阵的乘法,所有方法将返回操作后的结果矩阵。(两个矩阵的乘法:一个m×n的矩阵a(m,n)乘一个n×p的矩阵b(n,p),会得到一个m×p的矩阵c(m,p)。矩阵的行数和列数自定。)   
   c) 定义构造方法。     
     
2. 编写主类,测试Matrix类。包括:构建对象,测试每一个方法,并将测试结果输出到屏幕上。

输入格式:

按下面格式输入行数、列数和矩阵:
矩阵1:

2 3
1 2 3 
4 5 6

矩阵2:

3 2
7 8 
9 0
1 2

输出格式:

按下面格式输出乘积矩阵,每行后面有一个空格符和回车换行符:

28 14 
79 44 

输入样例:

2 3
1 2 3 
4 5 6
3 2
7 8 
9 0
1 2

输出样例:

28 14 
79 44 

import java.util.Scanner;

class Matrix{
    int [][] matrix;
    int row;
    int column;
    static Scanner in=new Scanner(System.in);
    Matrix(){}
    public static Matrix shuru() {
        Matrix m=new Matrix();
        m.row=in.nextInt();
        m.column=in.nextInt();
        m.matrix=new int [m.row] [m.column];
        for(int i=0;i<=m.matrix.length-1;i++) {
            for(int j=0;j<=m.matrix.length-1;j++) {
                m.matrix[i][j]=in.nextInt();
            }
        }
        return m;
    }
    public Matrix chengfa(Matrix m1,Matrix m2){
        Matrix m3=new Matrix();
        m3.row=m1.row;
        m3.column=m2.column;
        m3.matrix=new int [m3.row][m3.column];
        for(int i=0;i<=m1.row-1;i++) {
            for(int j=0;j<=m2.column-1;j++) {
                for(int k=0;k<=m1.column-1;k++) {
                    m3.matrix[i][j]+=m1.matrix[i][k]*m2.matrix[i][k];
                }
            }
        }
        return m3;
    }
}
class Main{
    public static void main(String [] agrs) {
        Matrix m1=Matrix.shuru();
        Matrix m2=Matrix.shuru();
        Matrix m3=new Matrix();
        m3.chengfa(m1,m2);
        display(m3);
    }
    public static void display(Matrix m3){
        for(int i=0;i<=m3.row-1;i++) {
            for(int j=0;j<=m3.column-1;j++) {
                System.out.print(m3.matrix[i][j]);
                System.out.print(" ");
            }
            System.out.printf("\n");
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值