2016-09-28 45 views
1

我搞砸了我的代碼,我仍然沒有找到答案。 Format.left只是一個在每個數字之間放置空格的類。我試圖通過4秀上側有1人數卻不斷出現這樣的:乘法表彈出

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

我希望我做的意義你們我的英語實在是太差了。

for(cols = 1; cols <= 4; cols++) 
    { 
     for (rows = 1; rows <= 6; rows++) 
     { 
      System.out.print(Format.right(cols * rows,7)); 
     } 
     System.out.println(); 
    } 

我在尋找的東西簡單地是這樣的:

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 
+1

我們不知道你想要代碼做什麼,也不會告訴我們。我們不可能提供幫助。請編輯該問題。 – nhouser9

+0

我已經糾正它,我希望它可以幫助更多,我解釋它真的很糟糕。 –

回答

0

試試這個:

for(cols = 0; cols <= 4; cols++) 
{ 
    for (rows = 0; rows <= 6; rows++) 
    { 
     if (rows == 0 || cols == 0) { 
      System.out.print(Format.right(cols + rows, 7)); 
     }else { 
      System.out.print(Format.right(cols * rows, 7)); 
     } 
    } 
    System.out.println(); 
} 

你想在每一行和列的一個,所以我乾脆開始轉移循環爲0而不是1,並添加了一個if語句來處理第一行/列的特殊情況。

+0

非常感謝!現在有一個美好的一天:)。這真的很難做,我會從中學習,謝謝! –

+0

@ThomasClassiFied如果這有助於請upvote並接受答案。 – nhouser9