通过多种方法传递二维数组

时间:2017-10-29 14:19:23

标签: java

我在为学校写作的课程遇到一些麻烦。本质上,我必须使用多个方法并通过它们传递二维数组。要做到这一点,我必须在方法中使用嵌套for循环,但方法不允许我返回" total"。你可以在下面看到我正在使用的方法。最底层的两种方法工作正常,只有总变量的方法没有。 (是的,我尝试过使用其他名称。)

//method sum of each row
public static int rowsum(int [][] matrix){
    System.out.println("Row Totals***************************************");
    for (int row = 0; row<matrix.length; row++){
        int total = 0;
        for (int column = 0; column<matrix.length; column++)
            total += matrix[row][column];
        System.out.println("The sum of row "+row+" is: "+total);
    }
    return total; 
}
//method sum of each column
public static int columnsum(int[][] matrix){
    System.out.println("Column Totals************************************");
    for (int column = 0; column<matrix[0].length; column++){
        int total = 0;
        for (int row = 0; row<matrix.length; row++){
            total += matrix[row][column];
        System.out.println("The sum of column "+column+" is: "+total);
        }
    return total;
    }
return total;
}
//method product if rows
public static int rowprod(int [][] matrix){
    System.out.println("Row Product**************************************");
    for (int row = 0; row<matrix.length; row++){
        int total = 0;
        for (int column = 0; column<matrix.length; column++)
            total *= matrix[row][column];
        System.out.println("The sum of column "+row+" is: "+total);
    }
return total;
}    
//method product of columns
public static int columnprod(int [][] matrix){
    System.out.println("Column Product***********************************");
    for (int column = 0; column<matrix[0].length; column++){
        int total = 0;
        for (int row = 0; row<matrix.length; row++)
            total = total*matrix[row][column];
        System.out.println("The sum of column "+column+" is: "+total);
    }
    return total;
}
//method highest value in matrix
public static int highest(int [][] matrix){
   System.out.println("This greatest value in this matrix is: ");
   int high = matrix [0][0];
   for (int row = 0; row<matrix.length;row++){
       for(int column=0;column<matrix.length; column++){
           if(high<matrix[row][column]){
               high=matrix[row][column];
           }
       }
   }
   return  high;
}
//method lowest value of matrix
public static int lowest(int [][] matrix){
   System.out.println("The lowest value in this matrix is: ");
   int low = matrix [0][0];
   for (int row = 0; row<matrix.length;row++){
       for(int column=0;column<matrix.length; column++){
           if(low>matrix[row][column]){
               low=matrix[row][column];
           }
       }
   }
   return low;
}

1 个答案:

答案 0 :(得分:2)

问题在于变量范围, 您的代码在这些地方存在缺陷

public static int columnsum(int[][] matrix){
System.out.println("Column Totals************************************");
for (int column = 0; column<matrix[0].length; column++){
    int total = 0; // total is defined within the for loop you cannot access it outside the for loop
    for (int row = 0; row<matrix.length; row++){
        total += matrix[row][column];
    System.out.println("The sum of column "+column+" is: "+total);
    }
return total; // Java do not know the total variable since it's already got destroyed after the for loop got terminated
}

如何纠正这个?

public static int columnsum(int[][] matrix){
System.out.println("Column Totals************************************");
int total = 0; // define total here
for (int column = 0; column<matrix[0].length; column++){

    for (int row = 0; row<matrix.length; row++){
        total += matrix[row][column];
    System.out.println("The sum of column "+column+" is: "+total);
    }
return total;
}

对所有完全出现故障的情况这样做