2015-10-19 55 views
0

我希望我的輸出像這樣,例如如果用戶輸入3: 不使用二維數組爲我的作業打印2個陣列,如矩陣

 1 2 3 

1 1 2 3 
2 1 4 6 
3 3 6 9 

到目前爲止我的代碼

public void matrixmutilplication() { 
    String thenumberofmatrix = JOptionPane.showInputDialog(null, "Enter the number of column and rows "); 
    int i = Integer.parseInt(thenumberofmatrix); 

    int[] cloumnarray = new int[i]; 
    int[] rowarray = new int[i]; 

    for (int z = 0; z <= i - 1; z++) { 
     cloumnarray[z] = z + 1; 
     rowarray[z] = z + 1; 
    } 

    for (int j = 0; j < i; j++) { 
     System.out.println(cloumnarray[j] * rowarray[j]); 
    } 
} 

我嘗試了不同的選項,並不能得到這個正常工作。

+2

如果有一個' 2'在位置(2,1)(行,列) – Flown

+0

爲了在最後打印,您需要嵌套for-loop來覆蓋列的打印。 –

回答

-1

每當你與涉及兩個數組(尤其是如果你想一個解決與模式有關係的問題)矩陣工作,你想有一個嵌套循環,像這樣:

for(int row = 0; row < numSelected; row++) { 
    for(int col = 0; col < numSelected; col++) { 
     ... 
    } 
} 

這樣,矩陣中的每個單元格都會被覆蓋。現在使用它,您可以嘗試乘以row索引和col索引並將其存儲到正確的單元格。希望這可以幫助,但讓我知道如果你需要額外的提示。

+1

我想指出你的輸出是基於零的。意思是第一行/列將全部爲零。 –

+0

我並不是真的想直接回答這個問題,而是向他展示了一個嵌套的for-loops爲自己解決問題的正確方法。不僅如此,我個人發現'array [row] [column]'然後'array [row + 1] [column + 1]'更方便。 –

+1

當然,我在評論OP的好處。 –

-1

所以這應該很簡單。

public void matrixmutilplication() { 
    String thenumberofmatrix = JOptionPane.showInputDialog(null, "Enter the number of column and rows "); 
    int i = Integer.parseInt(thenumberofmatrix); 

    for (int a = 0; a < i; a++) { 
     for (int b = 0; b < i; b++) { 
      System.out.print(a*b + "\t"); 
     } 
     System.out.print("\n"); 
    } 
} 
1
public static void matrixmutilplication() { 
    String thenumberofmatrix = JOptionPane.showInputDialog(null, "Enter the number of column and rows "); 
    int i = Integer.parseInt(thenumberofmatrix); 

    for (int a = 0; a <= i; a++) { 
     for (int b = 0; b <= i; b++) { 
      // top corner, don't print nothing 
      if (a == 0 && b == 0) System.out.print("\t"); 
      // top row 0-1, 0-2, 0-3 etc... just 1,2,3... 
      else if (a == 0) { 
       System.out.print(b + "\t"); 
       // last line, print extra line break 
       if (b == i) 
        System.out.print("\n"); 
      } 
      // first column 1-0, 2-0, 3-0... just a + space (tabulator) 
      else if (b == 0)  System.out.print(a + "\t"); 
      // any other cases, are candidates to multiply and give result 
      else     System.out.print(a*b + "\t"); 
     } 
     //look this is out of scope of nested loops, so, 
     // in each a iteration, print line break :) 
     System.out.print("\n"); 
    } 
} 

public static void main(String[] args) throws Exception { 
    matrixmutilplication(); 
} 

OUTPUT(3)

1 2 3 

1 1 2 3 
2 2 4 6 
3 3 6 9 

OUTPUT(5)

1 2 3 4 5 

1 1 2 3 4 5 
2 2 4 6 8 10 
3 3 6 9 12 15 
4 4 8 12 16 20 
5 5 10 15 20 25 

但(對我來說)問題是數字是不是在填充自然秩序,所以,達到你的目標,完全按照您的演示,將需要一點點填充這樣

public static void matrixmutilplication() { 
    String thenumberofmatrix = JOptionPane.showInputDialog(null, "Enter the number of column and rows "); 
    int i = Integer.parseInt(thenumberofmatrix); 

    for (int a = 0; a <= i; a++) { 
     for (int b = 0; b <= i; b++) { 
      if (a == 0 && b == 0) System.out.print("\t"); 
      else if (a == 0) { 
       System.out.print(String.format("%3s", b)); 
       if (b == i) 
        System.out.print("\n"); 
      } 
      else if (b == 0)  System.out.print(a + "\t"); 
      else     System.out.print(String.format("%3s", a*b)); 
     } 
     System.out.print("\n"); 
    } 
} 

public static void main(String[] args) throws Exception { 
    matrixmutilplication(); 
} 

OUTPUT(7)

 1 2 3 4 5 6 7 

1  1 2 3 4 5 6 7 
2  2 4 6 8 10 12 14 
3  3 6 9 12 15 18 21 
4  4 8 12 16 20 24 28 
5  5 10 15 20 25 30 35 
6  6 12 18 24 30 36 42 
7  7 14 21 28 35 42 49 

什麼看起來相當不錯:)