2013-08-24 107 views
0

我在編寫模擬植物生長的程序時有a question earlier。它基本上使用一個循環來計數達到預定的限制。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 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

+0

步驟通過它:您的問題可以容易地通過簡單地將可變移動到for循環導致它與值"branches"在循環的每次迭代進行初始化來解決。你將它設置爲1的「分支」,但當你> 1時,你不會回到「分支」。你使用的是IDE嗎?學習如何逐步完成代碼(調試)非常有用。 – keyser

+0

爲什麼不以'word = branch'開始,然後以'current_growth> 1'開頭,將其設置爲'branches'。它會更乾淨。 –

回答

0

您需要的字變量重置回「字」如果current_growth大於1.

0

好吧,如果你想有一個「快速和骯髒」的解決方案只需添加這else語句:

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

與你的程序的問題是,該變量word是個主要方法的範圍,而不是for-loop的範圍。這意味着如果您在一次迭代中修改for循環內的變量,它將在剩餘的迭代中保留該值。

/* 

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 
+0

我明白了。 我最終以不同的,更馬虎的方式修復了這個解決方案。您的解決方案更簡單,似乎遵循更好的編碼實踐: – Berzerkeley