我在編寫模擬植物生長的程序時有a question earlier。它基本上使用一個循環來計數達到預定的限制。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 plant1
{
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";
for(current_growth = 0; current_growth <= limit; ++current_growth)
{
//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
}
if (current_growth < 100)
{
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
唯一的問題是:
我添加了一個語法檢查,檢查單複數與問候的branches
數正在打印。
然而,當我運行代碼的檢查沒有工作,所以我有這樣的句子Your plant has grown 97 branch
時,它應該說Your plant has grown 97 branches
步驟通過它:您的問題可以容易地通過簡單地將可變移動到for循環導致它與值
"branches"
在循環的每次迭代進行初始化來解決。你將它設置爲1的「分支」,但當你> 1時,你不會回到「分支」。你使用的是IDE嗎?學習如何逐步完成代碼(調試)非常有用。 – keyser爲什麼不以'word = branch'開始,然後以'current_growth> 1'開頭,將其設置爲'branches'。它會更乾淨。 –