⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exercise5_23.java

📁 Introduction to java programming 一书中所有编程练习部分的源码
💻 JAVA
字号:
public class Exercise5_23 {  /** Main method */  public static void main(String[] args) {    // Create two matrices as two dimensional arrays    int[][] matrix1 = new int[5][5];    int[][] matrix2 = new int[5][5];    // Assign random values to matrix1 and matrix2    for (int i = 0; i < matrix1.length; i++)      for (int j = 0; j < matrix1[i].length; j++) {        matrix1[i][j] = (int)(Math.random() * 10);        matrix2[i][j] = (int)(Math.random() * 10);      }    // Multiply two matrices and print the result    int[][] resultMatrix = multiplyMatrix(matrix1, matrix2);    System.out.println("\nThe multiplication of the matrices is ");    printResult(matrix1, matrix2, resultMatrix, '*');  }  /** The method for multiplying two matrices */  public static int[][] multiplyMatrix(int[][] m1, int[][] m2) {    int[][] result = new int[m1.length][m2[0].length];    for (int i = 0; i < m1.length; i++)      for (int j = 0; j < result.length; j++)        for (int k = 0; k < result[0].length; k++)           result[i][j] += m1[i][k] * m2[k][j];    return result;  }  /** Print result */  public static void printResult(    int[][] m1, int[][] m2, int[][] m3, char op) {    for (int i = 0; i < m1.length; i++) {      for (int j = 0; j < m1[0].length; j++)        System.out.print(" " + m1[i][j]);      if (i == m1.length / 2)        System.out.print( "  " + op + "  " );      else        System.out.print( "     " );      for (int j = 0; j < m2[0].length; j++)        System.out.print(" " + m2[i][j]);      if (i == m1.length / 2)        System.out.print( "  =  " );      else        System.out.print( "     " );      for (int j = 0; j < m3[0].length; j++)        System.out.print(" " + m3[i][j]);      System.out.println();    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -