2014-05-05 54 views
0

我想我的答案讀可能無法認識到這正確

   1 2 3 
Matrix A =  1 2 3 
       1 2 3 

但它讀取

Matrix A = 
1 2 3 
1 2 3 
1 2 3 

誰能告訴我,我應該如何去試圖格式化?

public static void IO(int[][] a, int[][] b) 
    { 
     int i; 

     int j; 

     System.out.print("Please Enter the Numbers for Matrix A: "); 

     for(i=0;i<3;i++) 

     for(j=0;j<3;j++) 

      a[i][j]=scan.nextInt(); 

     System.out.println(); 

     System.out.print("Please Enter The Numbers for Matrix B: "); 

     for(i=0;i<3;i++) 

     for(j=0;j<3;j++) 

      b[i][j]=scan.nextInt(); 

     System.out.printf("%-1s%n", "Matrix A = "); 

     for(i=0;i<3;i++) 
     { 
     for(j=0;j<3;j++) 
     { 
      System.out.printf("%5d", a[i][j]); 
     } 
     System.out.println(); 
     } 
     System.out.printf("%-1s%n", "Matrix B = "); 


     for(i=0;i<3;i++) 
     { 
     for(j=0;j<3;j++) 
     { 
      System.out.printf("%5d", b[i][j]); 
     } 
     System.out.println(); 
     } 
    } 

我知道這對某些人來說可能看起來很簡單,但我現在只是編程了一個學期。任何意見是極大的讚賞!

回答

0

擺脫:

System.out.printf("%-1s%n", "Matrix A = "); 

,改變你的循環中:

for(i=0;i<3;i++) 
    { 
    for(j=0;j<3;j++) 
    { 
     if(j == 1) 
      line_prefix = "Matrix A = "; 
     else 
      line_prefix = " "; 
     System.out.printf("%12.12s%5d", lineprefix, a[i][j]); 
    } 
    System.out.println(); 
    }