簡體   English   中英

Java-語法檢查無法正常運行

[英]Java - Grammar check not operating properly

早些時候 ,我在編寫一個模擬植物生長的程序時遇到了一個問題 它本質上使用一個計數到預定極限的循環。

此后,我將代碼更改為以下內容:

/*

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

程序的問題在於變量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

如果current_growth大於1,則需要將word變量重置為“ words”。

好吧,如果您想要一個“快速而骯臟的”解決方案,只需添加以下else語句:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM