2015-12-08 31 views
0

程序應該使用嵌套for循環輸出以下內容,如果可能,最好不要使用pow()方法。任何幫助或建議被appreciated.Expected結果:權力表

1 1 1 1 1 1 

2 4 8 16 32 64 

3 9 27 81 243 729 

4 16 64 256 1024 4096 

我嘗試它:

class TableOfPowers 
{ 
    public static void main(String [] args) 
    { 
     int startValue = 1; 
     int y = 1; 
     for (int row =0; row < 4; row++) 
     { 
      for (int col = startValue; col < startValue+6; col ++) 
      { 
       y = y *startValue; 
       System.out.print(y + " "); 
      }  
      System.out.println(); 
      startValue++; 
     } 
    } 
} 
+0

問題是什麼?請具體說明。 – rgettman

+0

你期待什麼結果? – Perdomoff

+0

這是一項研究任務嗎?它看起來像是在作業或測試中的問題。 – mwotton

回答

0

看起來你需要在外環分配y = 1地方。

+0

你是發現,謝謝。即使我宣佈它不在循環之中,爲什麼還要這麼做? –

+0

@AndreasPoppmeier我編輯我的答案內循環 - >外循環。因爲y在內部循環中被改變。 –

1

得到它的工作,這是我的答案:

class TableOfPowers 
{ 

    public static void main(String [] args) 
    { 
     int startValue = 1; 

     for (int row =0; row < 4; row++) 
     { 
      int y =1; 

      for (int col = startValue; col < startValue+6; col ++) 
      { 

       y = y *startValue; 
       System.out.print(y + " "); 
      } 

      System.out.println(); 
      startValue++; 
     } 
    } 
}