2015-11-05 26 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(); 
    } 
} 

}