2015-09-07 100 views
-4

我一直在嘗試做對角線矩陣一段時間了,但我卡住了。它應該看起來像這樣,有兩個用戶輸入(左邊的負數和右邊的正數,而且它必須以零開始和結束,這就像我得到的那樣,我怎樣才能使用數組創建對角矩陣?開始編程,所以請裸跟我感謝您的幫助如何做對角線矩陣

-2 0 2 4 6

-6 -3 0 3 6

-12。 - 8 -4 0 4

-20 -15 -10 -5 0

public static void main(String[] args) { 

    Scanner skener = new Scanner (System.in); 



     int m, n; 

     do 
     { 
     System.out.print("m: "); 
     m = skener.nextInt(); 

     System.out.print("n: "); 
     n = skener.nextInt(); 


     }while(m>=10); 


     for (int x=1; x<=m; x++) { //ponavljajoča zanka za m 


      for (int y=1; y<=n; y++) { //ponavljajoča zanka za n 





       System.out.print(x*y); //zmnožek števcev (x in y) 
       System.out.print(" "); //gre v novo vrsto 

       if(n==5) { 
        System.out.print(" "); 
       } 
      } 
      System.out.println(); 
     } 

    } 


} 
+2

我不知道你正在嘗試做的,你能解釋清楚一點嗎?另外,你寫的代碼到底是什麼問題? – Keppil

回答

0

這不使用像您所談論的數組,但打印矩陣就像您的代碼示例一樣。

public static void main(String[] args) { 
    printDiagonalMatrix(5, 5); 
} 
public static void printDiagonalMatrix(int width, int height) { 
    for (int row = 0; row < height; row++) { 
     for (int col = 0; col < width; col++) { 
      if (col != 0) 
       System.out.print(' '); 
      System.out.print((col - row) * (row + 1)); 
     } 
     System.out.println(); 
    } 
} 

輸出

0 1 2 3 4 
-2 0 2 4 6 
-6 -3 0 3 6 
-12 -8 -4 0 4 
-20 -15 -10 -5 0 
+0

感謝您的幫助。正是我需要的:) – norcek10