2013-08-24 2 views
-1

식물이 자라는 방식을 시뮬레이션하기 위해이 아이디어를 생각해 냈습니다. 아이디어는 프랙탈 기하학을 기반으로하며 기본적으로 식물의 가지 및 기타 구조물의 반복을 설명합니다. 나는 프로그래밍에 초보자이기 때문에 뭔가를 반복하는 기본 논리 만 테스트해볼 것이라고 생각했다. 아래는 자바에서 쓴 것입니다.Java - 중첩 루프 - 출력을 얻는 데 문제가 있음

  1. 내 자바 보관소에 저장된 MacBook-Air:Java bdeely$ javac plant.java
  2. 로/컴파일 된 코드 호출이
  3. 를 가리 키도록 내 예탁을 변경 MacBook-Air:Java bdeely$ java plant으로 코드를 실행 :
    /* 
    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 
    

    나는 다음을했다

문제는 다음과 같습니다. - 출력이 전혀 없습니다. 명령 프롬프트가 비어있는 것처럼 돌아왔다. MacBook-Air:Java bdeely$

나는 거대한 루키 실수를하고있다. 아무도 내가이 코드를 얻을 수있는 줄 내게 0에서 최대 100 루프를 반복 할 줄 알아요?

감사합니다. current_growth 이후

+1

'while'루프 상태를 확인하십시오. 그것은 진실이 아니므로, 집행은 그것에 들어 가지 않습니다. –

답변

0

은 그것에서 시작되기 때문에 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

다른 방법을 이용해 주셔서 감사합니다. 나는 for 루프로 갈거야 – 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

관련 문제