2017-04-15 31 views
0

所以我在下面寫了這段代碼。我不知道它有什麼問題。我正在嘗試打印操作並做了一些操作。現在它不會打印。 這是提示。Java編碼COSC實驗室

編寫一個方法來添加兩個矩陣和一個方法來乘兩個矩陣。這兩種方法的標頭是:

public static double [] [] multiplyMatrix(double[][] a, double[][] b) 
public static double [] [] addMatrix(double[][] a, double[][] b) 

收件,提示用戶輸入中3×3矩陣和顯示輸出的測試程序。

this is the output

import java.util.Scanner; 

public class Assignment8{ 

private static Scanner sc; 
public static double[][] multiplyMatrix(double[][] c,double[][] d){ 
     double x[][]=new double[3][3]; 
     for(int i=0;i<3;i++) 
      for(int j=0;j<3;j++) 
       for(int k=0;k<3;k++) 
        x[i][j]=c[i][k]*d[k][j]; 
     return x;} 

public static double[][] addMatrix(double[][] a,double[][] b){ 
     double d[][]=new double[3][3]; 
     for(int i=0;i<3;i++) 
      for(int j=0;j<3;j++) 
       d[i][j]=a[i][j]+b[i][j]; 
     return d;} 

public static void main(String args[]){ 
     sc = new Scanner(System.in); 
     double a[][]=new double[3][3]; 
     double b[][]=new double[3][3]; 
     double c[][]=new double[3][3]; 
     double d[][]=new double[3][3]; 
     double sum[][]=new double[3][3]; 
     double mul[][]=new double[3][3]; 


     System.out.println("Enter the elements of matrix a:"); 
     for(int i=0;i<3;i++) 
      for(int j=0;j<3;j++) 
       a[i][j]=sc.nextInt(); 


     System.out.println("Enter the elements of matrix b:"); 
     for(int i=0;i<3;i++) 
      for(int j=0;j<3;j++) 
       b[i][j]=sc.nextInt(); 

     System.out.println("Enter the elements of matrix c:"); 
     for(int i=0;i<3;i++) 
      for(int j=0;j<3;j++) 
       c[i][j]=sc.nextInt(); 


     sum=addMatrix(a,b); 


     mul=multiplyMatrix(c,d); 
} 


     public static void printResult(              
       double[][] a, double[][] b, double[][] c, double[][] x, char op1, char op2) { 
      for (int i = 0; i < a.length; i++) { 
       for (int j = 0; j < a[0].length; j++) 
       System.out.print(" " + a[i][j]); 

       if (i == a.length/2) 
       System.out.print(" " + op1 + " "); 
       else 
       System.out.print("  "); 

       for (int j = 0; j < b[0].length; j++) 
       System.out.print(" " + b[i][j]); 

       if (i == a.length/2) 
       System.out.print(" " + op2 + " "); 
       else 
       System.out.print("  "); 

       for (int j = 0; j < c[0].length; j++) 
       System.out.print(" " + c[i][j]); 

       if (i == a.length/2) 
       System.out.print(" = "); 
       else 
       System.out.print("  "); 

       for (int j = 0; j < x [0].length; j++) 
       System.out.print(" " + x [i][j]); 

       System.out.println(); 
      } 
      } 
} 

回答

0

它看起來像您使用的ScannernextInt()方法而在第三矩陣讀書,但在圖像中的樣本來看,你輸入十進制值,而不是一個整數。

如果您希望能夠輸入十進制值,請嘗試使用nextFloat()nextDouble()

...另外,如果您希望顯示輸出,您應該在main()的某個點上調用printResult方法。

+0

它沒有改變任何東西,所以我想這不是問題。我發送該文件是一種流言。但其他的東西。即使你在輸入中輸入沒有小數的代碼,它仍然不會輸出數據 – AndyCoder

+0

@AndrewBourg並且添加對'printResult'的調用不會改變什麼? – APerson

+0

我很抱歉,我讀得太快了,我只看到了第一部分哈哈。但我該怎麼做。我還不太熟悉詞彙。所以我不完全確定你告訴我要放在主要方法 – AndyCoder