2015-11-05 58 views
0

我想添加兩個多數組在一起的數字,並在另一個多dim.-數組中打印出來,這裏是代碼我'到目前爲止,但它顯示了這個錯誤「不兼容的類型:double不能轉換爲double [] []」每次,任何人都可以請幫忙嗎?添加兩個多維數組,並在一個新的列表中打印生成的多數組JAVA

import java.util.*; 
public class AddMatrices 
{ 
public static void main(String[] args) 
{ 
    Scanner scan = new Scanner(System.in); 
    double[][] list1 = new double [3][3]; 
    double[][] list2 = new double [3][3]; 
    double[][] list3 = new double [3][3]; 

    System.out.println("Enter your first 3-by-3 matrix row by row:"); 
    for(int row = 0; row<3; row++){ 
    for(int col = 0; col<3; col++){ 
     list1[row][col] = scan.nextDouble(); 
    } 
    } 
    System.out.println(); 
    System.out.println("Enter your second 3-by-3 matrix row by row:"); 
    for(int row2 = 0; row2<3; row2++){ 
    for(int col2 = 0; col2<3; col2++){ 
     list2[row2][col2] = scan.nextDouble(); 
    } 
    } 
    System.out.print("The sum of these two matrices is: "); 
    for(int row3 = 0; row3<3; row3++){ 
    for(int col3 = 0; col3<3; col3++){ 
     list3[row3][col3] = addMatrix(list1[row3][col3],list2[row3][col3]); 
     System.out.println(list3[row3][col3] + " "); 
    } 
    System.out.println(); 
    } 
} 
public static double[][] addMatrix(double[][] a, double[][] b){ 
double[][] output = new double [a.length][a[0].length]; 
for(int row = 0; row<a.length; row++){ 
    for(int col = 0; col<a[row].length; col++){ 
     output[row][col] = a[row][col] + b[row][col]; 
    } 
    } 
    return output; 
} 
} 

更新後的代碼(它的工作原理耶非常感謝你們!):

import java.util.*; 
public class AddMatricies 
{ 
public static void main(String[] args) 
{ 
    Scanner scan = new Scanner(System.in); 
    boolean rep = false; 

    while(rep == false){ 

    double[][] list1 = new double [3][3]; 
    double[][] list2 = new double [3][3]; 
    double[][] list3 = new double [3][3]; 

    //asks for the first matrix 
    System.out.println("Enter your first 3-by-3 matrix row by row:"); 
    for(int row = 0; row<3; row++){ 
    for(int col = 0; col<3; col++){ 
     list1[row][col] = scan.nextDouble(); 
    } 
    } 
    //asks for the second matrix 
    System.out.println(); 
    System.out.println("Enter your second 3-by-3 matrix row by row:"); 
    for(int row2 = 0; row2<3; row2++){ 
    for(int col2 = 0; col2<3; col2++){ 
     list2[row2][col2] = scan.nextDouble(); 
    } 
    } 
    //add both matrices 
    System.out.println("The sum of these two matrices is: "); 
    for(int row3 = 0; row3<3; row3++){ 
    for(int col3 = 0; col3<3; col3++){ 
     list3 = addMatrix(list1,list2); //where the main changes are 
     System.out.print(list3[row3][col3] + " "); 
    } 
    System.out.println(); 
    } 
    System.out.println(); 
    System.out.print("Would you like to go again? "); 
    String dec = scan.next(); 
    if(dec.equalsIgnoreCase("yes")){ 
    rep = false; 
    } 
    else 
    rep = true; 
    } 
} 
//the method for adding both matrices 
public static double[][] addMatrix(double[][] a, double[][] b){ 
double[][] output = new double [a.length][a[0].length]; 
for(int row = 0; row<a.length; row++){ 
    for(int col = 0; col<a[row].length; col++){ 
     output[row][col] = a[row][col] + b[row][col]; 
    } 
    } 
    return output; 
} 
} 
+1

我打賭這是行:項目list3 [ROW3] [COL3] = addMatrix(list1的[ROW3] [COL3],列表2 [ROW3] [COL3]);您的addMatrix返回一個數組,您嘗試將其推入一個雙精度值... –

+0

下一次,請包含完整的編譯器錯誤消息。找出包含語法錯誤的確切行是很繁瑣的。 – Turing85

+0

此外,addMatrix採用數組作爲輸入,所以沒有很好的傳遞double值...「double不能轉換爲double [] []」 –

回答

0

你addMatrix方法期望數組作爲參數和你逝去的雙...作爲T.Hry正確地指出,這是線。

+0

解決這個問題將繼續...只是改變雙[] []「不能轉換爲雙): –

+0

哦,認爲問題是錯誤每次都是一樣的,這可以通過提供一個不同的錯誤來解決它:)但是,必須更具體 - 解決變化參數加倍。 – vladz

0

只要避免使用addMatrix方法,因爲它不會爲代碼增加值併產生問題。代碼應該是這樣的:

import java.util.*; 
public class AddMatrices 
{ 
public static void main(String[] args) 
{ 
    Scanner scan = new Scanner(System.in); 
    double[][] list1 = new double [3][3]; 
    double[][] list2 = new double [3][3]; 
    double[][] list3 = new double [3][3]; 

    System.out.println("Enter your first 3-by-3 matrix row by row:"); 
    for(int row = 0; row<3; row++){ 
    for(int col = 0; col<3; col++){ 
     list1[row][col] = scan.nextDouble(); 
    } 
    } 
    System.out.println(); 
    System.out.println("Enter your second 3-by-3 matrix row by row:"); 
    for(int row2 = 0; row2<3; row2++){ 
    for(int col2 = 0; col2<3; col2++){ 
     list2[row2][col2] = scan.nextDouble(); 
    } 
    } 
    System.out.print("The sum of these two matrices is: "); 
    for(int row3 = 0; row3<3; row3++){ 
    for(int col3 = 0; col3<3; col3++){ 
     list3[row3][col3] = list1[row3][col3]+list2[row3][col3]; 
     System.out.println(list3[row3][col3] + " "); 
    } 
    System.out.println(); 
    } 
} 

}