java - 打印 2 个数组的行和列的总和

标签 java arrays sum

我完全被困住了。任何帮助,将不胜感激。问题提出

1.Create two 2-dim arrays/matrices (random numbers, range 1 -500, where 1 and 500 should be declared as class constants). Both have the same dimensions.

2.Print the arrays.

3.Call a method to sum the 2 matrices. The sum of 2 matrices matrix_1 and matrix_2 is a matrix result, where for every row r and column c, result_rc= matrix_1_rc+ matrix_2_rc

4.Print the resulting matrix.

我陷入了 3 和 4 的困境。部分问题是我不明白 3 所要求的逻辑。我是否获得每行和每列的总和并将其添加到第二个数组的行和列的总和中。第二个问题是我完全不知道下一步该做什么。

我已经研究了几个小时并尝试了不同的事情。

import java.util.*;

public class Lab12p2 {
    public static final int MAX = 500;
    public static final int MIN = 1;

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the number of times to run the program");
        int start = scan.nextInt();
        while (start < 0) {
            System.out.println("Error! Should be positive. REENTER");
            start = scan.nextInt();
        }
        // end number of times validation
        while (start > 0)// counter loop for how many times to run program {
            System.out.println("Enter the size of the 2 arrays");
        int SIZE = scan.nextInt();
        while (SIZE < 0) {
            System.out.println("Error! Should be positive. REENTER");
            SIZE = scan.nextInt();
        } // size validation
          // start of methods
        int[][] a = new int[SIZE][SIZE];
        int[][] b = new int[SIZE][SIZE];// second array
        System.out.println("The first array is ");
        System.out.println();
        randArray(a, SIZE, SIZE, MIN, MAX);
        System.out.println("The second array is ");
        System.out.println();
        randArray(b, SIZE, SIZE, MIN, MAX);
        sum2arrays(a, b, SIZE, SIZE);
        start--;
    }

    public static void randArray(int[][] matrix, int row, int col, int low, int up) {
        Random rand = new Random();
        for (int r = 0; r < row; r++) {
            for (int c = 0; c < col; c++) {
                int random = matrix[r][c] = rand.nextInt(up - low + 1) + low;
                System.out.print("\t" + random);
            }
            System.out.println();
        }
    }

    public static void sum2arrays(int[][] matrix1, int[][] matrix2, int col, int row) {
        int sumrow;
        int sumtotalrow = 0;
        for (int r = 0; r < matrix1.length; r++) {
            sumrow = 0;
            for (int c = 0; c < matrix1[r].length; c++) {
                sumrow = sumrow + matrix1[r][c];
            }
            sumtotalrow = sumtotalrow + sumrow;
        }
        // testing
        System.out.println("The sum of ALL the elements/row  = " + sumtotalrow);
    }
}

应该 调用一个方法来对 2 个矩阵求和。 2 个矩阵 matrix_1matrix_2 的总和是一个矩阵结果,其中对于每个 行 r列 cresult_rc=matrix_1_rc+matrix_2_rc(我不知道这意味着什么),然后打印结果矩阵

最佳答案

public static void sum2arrays(int[][] matrix1, int[][]matrix2,int col,int row)
{
int sumrow;
ArrayList <Integer> three=new ArrayList<Integer>();
for (int r = 0; r < matrix1.length; r++)
{ 
    sumrow = 0; 
    for (int c = 0; c < matrix1[r].length; c++)
    { 
        three.add( matrix2[r][c] + matrix1[r][c]); 
    }




  } 
        System.out.println("The matrix result is ");
         System.out.println(three);


 }

关于java - 打印 2 个数组的行和列的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56177847/

相关文章:

java heap size error dex2jar android反编译解决方法?

php - 阵列克隆的性能

php 数组只有一种类型

google-sheets - 在 Google 表格中,您可以使用 WEEKNUM() 和 YEAR() 等特定条件来计算行数吗?

java - 找不到 gradle 包装器

java - 如何使用 getClass().getResource() 方法

mysql - 从 MYSQL 查询中计算列的平均值

mysql - 如何将 "sickness time"汇总到命令末尾 - mysql

java - cometd 点对点

arrays - 如何使用带 Angular 复选框过滤对象数组?