2017-10-29 11 views
2

私は学校向けのプログラムに少し問題があります。基本的には、複数のメソッドを使用して、2次元配列を渡す必要があります。そのためには、メソッドのネストされたforループを使用する必要がありますが、メソッドでは「合計」を返さないようにしています。私は以下で使用している方法を見ることができます。下の2つのメソッドは正常に動作していますが、合計変数を持つメソッドは正しくありません。 (はい、私は他の名前を使用して試してみました。)複数の方法で2次元配列を渡す

//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

すべての場合にこれを行います。それらをループ外に定義します。 – sinclair

+2

[forループ内で宣言された変数のスコープ]の可能な複製(https://stackoverflow.com/questions/19463138/scope-of-variable-declared-inside-a-for-loop) – sinclair

答えて

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; 
} 

`total`変数は、forループの内側にのみ表示され、総故障

関連する問題