Java - Grammar check not operating properly

692 views Asked by At

I had a question earlier on a program I was writing to simulate plant growth. It essentially uses a loop that counts up to a predetermined limit.

I have since changed my code to the following:

/*

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

The only problem is the following:

I added a grammatical check to check for singular and plural with regards to the number of branches being printed.

However, when I run the code the check doesn't work so I have sentences like Your plant has grown 97 branch when it should say Your plant has grown 97 branches

3

There are 3 answers

0
tommy__123__ On

You need to reset the word variable back to "words" if the current_growth is greater than 1.

0
Bruno_Ferreira On

Well, if you want a "quick and dirty" solution just add this else statement:

 if (current_growth == 1)
 {
    word = "branch"; //this changes the "word" to branch instead of branches
 } 
 else {
    word = "branches";
 }
1
Alex Stockinger On

The problem with your program is that the variable word is in the scope of the main method, not of the for-loop. That means that if you modify the variable inside the for loop in one iteration it will retain this value over the remaining iterations. Your problem can easily be solved by simply moving the variable into the for-loop which causes it to be initialized with the value "branches" on each iteration of the loop:

/*

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