2013-08-24 37 views
-1

我想出了這個想法來模擬植物生長的方式。這個想法是基於分形幾何學,基本上是如何解釋植物中分支和其他結構的重複。不過,我非常喜歡編程的初學者,所以我想我只會測試重複某些事情的基本邏輯。以下是我用Java編寫的內容。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?

謝謝!

+1

檢查你的'while'循環條件。這不會是真的,因此執行不會進入它。 –

回答

0

由於current_growth被初始化爲0

while (current_growth > 0) // never becomes true 
0

這是因爲在啓動current_growth = 0但是當條件是(current_growth > 0) 你爲什麼不使用for循環:

for(current_growth = 0; current_growth < limit; ++current_growth) 
+0

感謝您的替代方法。我要去與循環 – Berzerkeley

0

current_growth爲0使其在>= 0

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

你可以使用和享受:) – Tushar

+0

感謝您的! 它甚至沒有在我的腦海中使用<= – Berzerkeley