集册 Java实例教程 将两个矩阵相加

将两个矩阵相加

欢马劈雪     最近更新时间:2020-01-02 10:19:05

595
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
将两个矩阵相加

import java.util.Scanner;


public class Main {
//N o w J a v a . c o m - 时代Java 提 供

  public static int[][] mMatrixSum(int mArrayA[][], int mArrayB[][]) {

    int i, j;


    int mRows = mArrayA.length;

    int mColumn = mArrayA[0].length;

    int[][] mSumArray = new int[mRows][mColumn];

    for (i = 0; i < mRows; i++) {

      for (j = 0; j < mColumn; j++) {

        mSumArray[i][j] = mArrayA[i][j] + mArrayB[i][j];

      }

    }

    return mSumArray;

  }
/** 时   代    Java - nowjava.com 提供 **/

  public static void main(String args[]) throws Exception {

    int r, c;

    int i, j;


    int mRow = 4;

    int mColumn = 3;

    int[][] mArrayA = new int[mRow][mColumn];

    for (r = 0; r < mArrayA.length; r++) {

      for (c = 0; c < mArrayA[r].length; c++) {

        mArrayA[r][c] = c + r;

        System.out.print(mArrayA[r][c] + " ");


      }

      System.out.println(" ");

    }

    int mRowTwo = 4;

    int mColumnTwo = 3;

    int[][] mArrayB = new int[mRowTwo][mColumnTwo];

    for (r = 0; r < mArrayB.length; r++) {

      for (c = 0; c < mArrayB[r].length; c++) {

        mArrayB[r][c] = r*c;

        System.out.print(mArrayB[r][c] + " ");

      }

      System.out.println(" ");

    }


    
展开阅读全文