(JAVA练习题)输入一个 m*n的矩阵 和一个n*p的矩阵,并初始化, 要求打印出两个矩阵的乘积.

import javax.sound.midi.Soundbank;
import java.util.Scanner;

//1.   输入一个 m*n的矩阵 和一个n*p的矩阵,并初始化, 要求打印出两个矩阵的乘积
public class Test01 {
     
    public static void main(String[] args) {
     
        System.out.println("请输入m行n列和n行p列的矩阵");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入m");
        int m = sc.nextInt();//输入m
        System.out.println("请输入n");
        int n = sc.nextInt();//输入n
        System.out.println("请输入p");
        int p = sc.nextInt();//输入p
        int array[][] = new int[m][n];//array矩阵为m行n列
        int array1[][] = new int[n][p];//array1矩阵为n行m列
        for (int i= 0; i < m; i++){
     
            for (int j = 0; j < n; j++){
     
                array[i][j] = sc.nextInt();//向矩阵array中输入数据
                System.out.print(array[i][j] + " ");//打印出矩阵数据
            }
            System.out.println();
        }
        for (int i= 0; i < n; i++){
     
            for (int j = 0; j < p; j++){
     
                array[i][j] = sc.nextInt();//向矩阵array1中输入数据
                System.out.print(array1[i][j] + " ");//打印出矩阵数据
            }
            System.out.println();
        }
        int array2[][] = new int[m][p];//创建矩阵用于存放乘积
        for (int i = 0; i < m; i ++){
     //矩阵行控制量
            for (int j = 0; j < p; j++){
     //局阵列控制量
                for (int k = 0; k < n; k++){
     //矩阵array2的列
                    array2[i][j] = array[i][k] * array1[k][j];//给矩阵array2赋值
                }
            }
        }
        for (int i = 0; i < m; i++){
     
            for (int j = 0; j < p; j++){
     
                System.out.print(array2[i][j] + " ");
            }
            System.out.println();
        }
    }
}

你可能感兴趣的:(JAVA练习题,java)