2017-03-03 43 views
0

我想製作一個「三角」乘法表。它必須有15行和10列,沒有重複。必須使用循環。我很難找出這個問題。我留下了額外的專欄。請幫忙!試圖製作一個三角形的Java Multiplucation表

Here is what it is supposed to look like

public class q2 { 

    public static void main(String[] args) { 

     final int jMax = 15; 
     final int iMax = 10; 

     System.out.println(""); 
     System.out.print(" | "); 

      for (int column = 1; column <= iMax; column++) 

      System.out.print(column + "\t"); 

      System.out.println(); 


      System.out.print("____________________________________________________________________________"); 

      System.out.println(); 

      for (int i = 1; i <= jMax; i++) 
      {  
       if (i>9) 
       { 
      System.out.print(i + " | "); 
       } 
       else 
        System.out.print(i + " | "); 

      for (int row = 1; row <=i; row++) { 

       System.out.print(i*row + " "); 
      } 
      System.out.println(); 

      } 
     } 

} 
+2

它現在做了什麼? – Matt

+0

完美,現在感謝! – Mike

回答

0

你有額外列的原因是這條線的位置:

for (int row = 1; row <=i; row++) { 
    System.out.print(i*row + " "); 
} 

這會讓你打印出一個多列在它之外的for循環的每次迭代(for (int i = 1; i <= jMax; i++))要解決此問題,可以使用三元運算符,它們本質上是一行中的if語句。如果您改爲使用for-loop的條件
row <=(i < 10 ? i :10),那麼您將修復每次迭代打印出新列的問題。在for循環,它看起來像這樣:

for (int row = 1; row <= (i < 10 ? i :10); row++) { 
    System.out.print(i*row + " "); 
} 

你可能仍然有問題與格式,但是這能解決您的問題與行。