简体   繁体   English

比较二维数组,然后添加它们

[英]comparing 2d arrays and then add them

import java.lang.Math;

public class Homework2 {
public static void main(String[] args){

    int d1 = (int) (Math.random()*(10-3+1)+3);
    int d2 = (int) (Math.random()*(10-3+1)+3);

    double[][] doubMatrix1 = new double[d1][d2];
    double[][] doubMatrix2 = new double[d1][d2];
    double[][] doubMatrix3 = new double[d1][d2];

    doubMatrix1 = getdoubMatrix(d1,d2);
    doubMatrix2 = getdoubMatrix(d1,d2);
    doubMatrix3 = addMatrices(doubMatrix1, doubMatrix2);
}
public static double[][] getdoubMatrix(int d1, int d2){

    double[][] tempArray = new double[d1][d2];

    for(int i =0; i <tempArray.length;i++ )
        for(int j =0;j < tempArray[i].length;j++)
            tempArray[i][j] = Math.random()*(10.0);

    return  tempArray;
}
public static double[][] addMatrices(double doubMatrix1[][], double doubMatrix2[][]){

    for(int i = 0; i< doubMatrix1.length;i++)
        for(int j = 0; j< doubMatrix1[i].length;j++ )
        {
            if(doubMatrix1[i][j] == doubMatrix2[i][j])
            {
                double[][] tempArray = new double[i][j];

            }
            else
            {
                return tempArray[0][0]; 
            }
        }

    return tempArray;
}
}

I'm getting error on both return statements in the addMatrices method also I don't think I'm doing it right我在 addMatrices 方法中的两个 return 语句上都遇到错误,而且我认为我做得不对

This is what i was supposed to do for the addMatrices method这就是我应该为 addMatrices 方法做的事情

In the addMatricesmethod,在 addMatrices 方法中,

· Check if the first dimensions and thesecond dimensions of each 2-dim. · 检查每个二维码的第一维和第二维是否一致。 array array are the same -- if they are NOTthe same, return a 0 X 0 2-dim.数组数组是相同的——如果它们不相同,则返回一个 0 X 0 2-dim。 array, otherwise do the following;数组,否则执行以下操作;

· Allocate memory for a local 2-dim. · 为本地2-dim 分配内存。 arraywith the same dimensions as one of the 2-dim.与二维数组之一具有相同维度的数组。 array parameters数组参数

· Add each corresponding element in theparameter 2-dim. ·在参数2-dim中添加每个对应的元素。 arrays and store the result in the corresponding element ofthe local 2-dim.数组并将结果存储在局部二维的相应元素中。 array (use nested for loops)数组(使用嵌套 for 循环)

· Return the local 2-dim. ·返回本地2-dim。 array大批

public static double[][] addMatrices(double doubMatrix1[][], 
                                     double doubMatrix2[][]){

for(int i = 0; i< doubMatrix1.length;i++)
    for(int j = 0; j< doubMatrix1[i].length;j++ )
    {
        if(doubMatrix1[i][j] == doubMatrix2[i][j])
        {
            double[][] tempArray = new double[i][j];

        }
        else
        {
            return tempArray[0][0]; 
        }
    }

return tempArray;
}
  • Well the first problem is that, in your else part you are returning a double value.那么第一个问题是,在您的 else 部分,您返回的是一个双精度值。 ArrayElement, while the return type is defined as an array of array . ArrayElement,而返回类型定义为array of array

  • Secondly, you have declared your tempArray inside your if and are using it outside, while returning.. It would not be visible outside your if .其次,你声明了tempArray内你if和被外面使用它,而返回。它不会是外部可见的if Declare it in your method, outside your if and for-loop.在你的方法中声明它,在你的 if 和 for 循环之外。

PRIMARY MODIFICATION NEEDED : -需要的主要修改: -

  • Change your return statment in your else to: -将 else 中的退货声明更改为:-

     tempArray = new double[0][0];
  • And declare your tempArray outside your for-loop.并在 for 循环之外声明您的tempArray

Well, there is lot more than the above problem.嗯,还有很多比上述问题。 Your addMatrix is logically not adding your matrices.您的addMatrix在逻辑上不会添加您的矩阵。 I think you should check onto that code..我认为您应该检查该代码..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM