-1

我想出了这个想法来模拟植物的生长方式。这个想法是基于分形几何,基本上它是如何解释植物中树枝和其他结构的重复的。不过,我是一个编程初学者,所以我想我只是测试一下重复某些东西的基本逻辑。下面是我用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 plant
{
    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";

        while (current_growth > 0)
        {
            //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
            }


        System.out.println("Your plant has grown " + current_growth + " " + word);
        //if you put the singular count check here, it doesn't work at all
        current_growth = current_growth + 1;

            if (current_growth > 0)
            {
            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

我做了以下事情:

  1. 将其保存在我的 Java 存储库中 将我的存储库更改为指向那里
  2. 调用/编译代码MacBook-Air:Java bdeely$ javac plant.java
  3. 运行代码MacBook-Air:Java bdeely$ java plant

问题是: - 根本没有输出。命令提示符刚刚回到空,就像MacBook-Air:Java bdeely$

我相当肯定我在这里犯了某种巨大的新手错误。有谁知道我怎样才能让这段代码给我输出从 0 到 100 重复循环的输出?

谢谢!

4

4 回答 4

1

编辑:

 public class plant
    {
        public static void main (String [] poop)
        {
            int current_growth = 0, limit = 1;//set limit to 1 //plant growth starts at ZERO and is limited to 100
        String word = "branches";

    while (limit<=100)// for 100 iterations limit
    {
        //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
        }


    System.out.println("Your plant has grown " + current_growth + " " + word);
    //if you put the singular count check here, it doesn't work at all
    current_growth = current_growth + 1;

        if (current_growth > 0)
        {
        System.out.println("Your plant has grown " + current_growth + " " + word);
        }
        else
        {
        System.out.println("Your plant will no longer grow.");
        } // end else
       limit=limit+1;// increment
    } //end while loop
} //end main method
} //end class
于 2013-08-24T21:25:29.023 回答
0

由于current_growth被初始化为0

while (current_growth > 0) // never becomes true
于 2013-08-24T21:22:56.113 回答
0

这是因为在开始时你的current_growth = 0但 while 条件是(current_growth > 0) 你为什么不使用for循环:

for(current_growth = 0; current_growth < limit; ++current_growth)
于 2013-08-24T21:23:35.827 回答
0

current_growth是 0.>= 0一会儿做

于 2013-08-24T21:23:56.643 回答