0

植物の成長をシミュレートするために書いていたプログラムについて、以前に質問がありました。基本的に、あらかじめ決められた制限までカウントするループを使用します。

その後、コードを次のように変更しました。

/*

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

4

3 に答える 3

1

プログラムの問題は、変数wordが for ループではなく、メイン メソッドのスコープ内にあることです。つまり、1 回の繰り返しで for ループ内の変数を変更すると、残りの繰り返しでこの値が保持されます。問題は、変数を for ループに移動するだけで簡単に解決できます。これにより、変数"branches"はループの各反復で値で初期化されます。

/*

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

        for(current_growth = 0; current_growth <= limit; ++current_growth)
        {
            String word = "branches";
            //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
于 2013-08-24T21:47:15.257 に答える
0

current_growth が 1 より大きい場合は、word 変数を「words」にリセットする必要があります。

于 2013-08-24T21:45:41.850 に答える
0

ええと、「手っ取り早い」ソリューションが必要な場合は、次の else ステートメントを追加するだけです。

 if (current_growth == 1)
 {
    word = "branch"; //this changes the "word" to branch instead of branches
 } 
 else {
    word = "branches";
 }
于 2013-08-24T21:46:49.480 に答える