我想出了這個想法來模擬植物生長的方式。這個想法是基於分形幾何學,基本上是如何解釋植物中分支和其他結構的重複。不過,我非常喜歡編程的初學者,所以我想我只會測試重複某些事情的基本邏輯。以下是我用Java編寫的內容。Java - 嵌套循環 - 無法獲得任何輸出
/*
Java program: plant.java
This program runs a loop simulating plant growth.
The idea is that the plant will continue to have "repeated"
growth and that it will stop (or slow down to a crawl) at some point,
in this case at 100 branches.
*/
public class plant
{
public static void main (String [] poop)
{
int current_growth = 0, limit = 100; //plant growth starts at ZERO and is limited to 100
String word = "branches";
while (current_growth > 0)
{
//here, we are checking to see if the plant has grown just 1 inch.
//this is important just for grammatical purposes
if (current_growth == 1)
{
word = "branch"; //this changes the "word" to branch instead of branches
}
System.out.println("Your plant has grown " + current_growth + " " + word);
//if you put the singular count check here, it doesn't work at all
current_growth = current_growth + 1;
if (current_growth > 0)
{
System.out.println("Your plant has grown " + current_growth + " " + word);
}
else
{
System.out.println("Your plant will no longer grow.");
} // end else
} //end while loop
} //end main method
} //end class
我做了以下內容:
- 在我的Java存保存它改變了我的存款指向 有
- 調用/編譯的代碼與
MacBook-Air:Java bdeely$ javac plant.java
- 與
MacBook-Air:Java bdeely$ java plant
冉代碼
問題是: - 完全沒有輸出。命令提示符剛剛回到空像MacBook-Air:Java bdeely$
我很確定我在這裏犯了一種巨大的菜鳥錯誤。有誰知道我怎麼能得到這個代碼給我輸出,重複循環從0到100?
謝謝!
檢查你的'while'循環條件。這不會是真的,因此執行不會進入它。 –