矩阵加法

时间:2017-02-07 13:22:01

标签: java arrays matrix addition

所以我必须为我的ap类制作一个矩阵计算器,但是当我将矩阵添加到一起时它没有正确添加,它只添加每个数组的第一个数字。请帮忙

import java.util.Scanner;
public class MatricAddition {



public static void main(String[] args) {
   Scanner s = new Scanner(System.in);
   System.out.print("Enter number of rows: ");
   int rows = s.nextInt();
   System.out.print("Enter number of columns: ");
   int columns = s.nextInt();
   int[][] a = new int[rows][columns];
   int[][] b = new int[rows][columns];
   System.out.println("Enter the first matrix");
   for (int i = 0; i < rows; i++) {
       for (int j = 0; j < columns; j++) {
           a[i][j] = s.nextInt();
       }
   }
   System.out.println("Enter the second matrix");
   for (int i = 0; i < rows; i++) {
       for (int j = 0; j < columns; j++) {
           b[i][j] = s.nextInt();
       }
   }
   int[][] c = new int[rows][columns];
   for (int i = 0; i < rows; i++) {
       for (int j = 0; j < columns; j++) {
           c[i][j] = a[i][j] + b[i][j];
       }
   }
   System.out.println("The sum of the two matrices is");
   for (int i = 0; i < rows; i++) {
       for (int j = 0; j < columns; j++) {
           System.out.print(c[i][j] + " ");
       }
       System.out.println();
   }
  }
 }

提前感谢您的帮助。 Output

0 个答案:

没有答案