早些时候我有一个关于我正在编写的模拟植物生长的程序的问题。它本质上使用一个循环计数到一个预定的限制。
此后,我将代码更改为以下内容:
/*
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