2013-08-24 1 views
0

저는 식물 성장을 시뮬레이션하려고 작성한 프로그램에 a question earlier을 가지고있었습니다. 본질적으로 미리 결정된 한도까지 카운트하는 루프를 사용합니다.Java - 문법 검사가 제대로 작동하지 않습니다.

난 이후로 내 코드를 변경 한 다음

내가 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 
     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 

유일한 문제는 다음과 인쇄되고있다. 나는이 코드를 실행하면

그러나

체크는 당신은 current_growth보다 큰 경우 다시 "단어"로 단어 변수를 재설정해야 Your plant has grown 97 branches

+0

단계 : 문제는 쉽게 간단히는 루프의 각 반복에 "branches" 값으로 초기화하는 원인에 대한 루프 변수를 이동시킴으로써 해결 될 수있다. 당신은 1에서 "branch"로 설정하지만,> 1 일 때는 "branches"로 돌아 가지 않을 것입니다. IDE를 사용하고 있습니까? 코드 (디버그)를 단계별로 실행하는 방법을 배우는 것이 좋습니다. – keyser

+0

'word = branch'로 시작한 다음'current_growth> 1'로'branches'로 설정하십시오. 훨씬 더 깨끗합니다. –

답변

0

말을해야 할 때 내가 Your plant has grown 97 branch 같은 문장이 그렇게 작동하지 않는다 1.

0

글쎄, 당신은 "신속하고 더러운"솔루션 그냥이 다른 문을 추가하려면 :

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

프로그램의 문제는 변수 word이 일에이다 e for 루프가 아닌 main 메소드의 범위. 즉, 하나의 반복에서 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

관련 문제