植物の成長をシミュレートするために書いていたプログラムについて、以前に質問がありました。基本的に、あらかじめ決められた制限までカウントするループを使用します。
その後、コードを次のように変更しました。
/*
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