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在 main 方法的范围内,而不是 for 循环的范围内。这意味着如果您在一次迭代中修改 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 回答