2016-05-31 127 views
-1

回答:倒掛等腰三角形

他們給我的任務是寫一個輸出基於用戶輸入的等腰三角形的Java程序。舉例來說,如果用戶輸入要提示的5號後,該方案將輸出

***** 
**** 
*** 
** 
* 

我們被告知while循環,但我沒有用任何的成功使用。我決定使用循環代替,但仍然有麻煩。我宣佈了我的變量並提示用戶輸入。下面你會發現我的循環。請幫忙!我所有的程序都在打印一連串的英鎊符號。

//For loop 
    for (CountOfRows=UserInput; CountOfRows>0; CountOfRows--) 
    { 
     System.out.println("# "); 
     for (CountOfColumns=UserInput; CountOfColumns>0; CountOfRows++) 
     { 
      System.out.println("# "); 
     } 

    } 
+2

'println'打印出你給它的字符串加上一個換行符。 – Taelsin

回答

2

如果您想使用while循環,你可以簡單地這樣做:

while(num > 0){ //stay in loop while the number is positive 
    int temp = num; //make a copy of the variable 
    while(temp-- > 0) //decrement temp each iteration and print a star 
     System.out.print("*"); //note I use print, not println 
    System.out.println(); //use println for newline 
    num--; //decrement number 
} 
0

你需要,使其運行,直至指數從最終改變你的內部for循環第一個循環是這樣的:

int num = 5; 

    for (int i = num; i > 0; i--) { 
     for (int j = 0; j < i; j++) { 
      System.out.print("*"); 
     } 
     System.out.println(); 
    }