Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
182 views
in Technique[技术] by (71.8m points)

if statement - Java - Nested loops - Having trouble getting any output

I came up with this idea to simulate the way a plant grows. The idea is based off of fractal geometry and basically how that explains the repetitions of branches and other structures in plants. I am very much a beginner with programming, though, so I thought I'd test out JUST the basic logic of repeating something. Below is what I wrote in 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

I did the following:

  1. Saved it in my Java depository Changed my depository to point to there
  2. Called/compiled the code with MacBook-Air:Java bdeely$ javac plant.java
  3. Ran the code with MacBook-Air:Java bdeely$ java plant

The problem was that: - There was no output at all. The command prompt just went back empty like MacBook-Air:Java bdeely$

I'm fairly certain I am making some kind of huge rookie mistake here. Does anyone know how I can get this code to give me output that repeats the loop from 0 up to 100?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Edited :

 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...