2013-08-06 2 views
1

while 문이 작동하지 않는 이유를 알아낼 수 있습니까? 루프는 i = 3 후에 멈추지 만 continueSurvey = 0이면 멈추지 않습니다. continueSurvey를 O로 변경하면 루프가 종료되지 않습니다. 프로세스로 들어가도 변수가 있음을 알 수 있습니다. 0이면 루프가 계속됩니다. 당신은 사용자가 계속하고자하는 경우 요청While 루프를 여러 조건으로 사용하는 경우

import java.util.Scanner; 

public class SurveyConductor 
{ 

    public static void main(String[] args) 
    { 
     Survey a = new Survey(); 
     a.display(); 

     a.enterQuestions(); 

     int continueSurvey = 1; 
     int i = 0; 

      while ((continueSurvey != 0) && (i < 3)) 
      { 

       for (int row = a.getRespID(); row < 3; row++) 
       { 

        System.out.println("Respondent " + (row+1) + " Please tell us how you would rate our: "); 

        for (int col = 0; col < 3; col++) 
        { 
         Scanner input = new Scanner(System.in); 

         System.out.println(a.presentQuestion(col) + ": "); 
         System.out.println("Enter your response (1-Strongly Disagree, 2-Disagree, 3-Neutral, 4-Agree, 5-Strongly Agree): "); 
         int response = input.nextInt(); 

         if ((response < 1) || (response >5)) 
         { 
          while ((response < 1) || (response > 5)) 
          { 
           System.out.println("Your response must be between 1 and 5. Please try again."); 

           System.out.println(a.presentQuestion(col) + ": "); 
           System.out.println("Enter your response (1-Strongly Disagree, 2-Disagree, 3-Neutral, 4-Agree, 5-Strongly Agree): "); 
           response = input.nextInt(); 
          } 
         } 

         a.logResponse(row,col,response); 
         System.out.println(); 
        } 

        a.displaySurveyResults(); 
        System.out.println(); 
        System.out.println("The top rated question is Question #" + a.topRatedQuestion() + "."); 
        System.out.println("The bottom rated question is Question #" + a.bottomRatedQuestion() + "."); 
        System.out.println(); 

        Scanner input2 = new Scanner(System.in); 
        System.out.println("Are there any more repondents (0 - No, 1 - Yes): "); 
        continueSurvey = input2.nextInt(); 


        a.generateRespondentID(); 
        i++; 
       } 
      } 
    } 
} 

답변

0

부분은

for (int row = a.getRespID(); row < 3; row++) 

아니라 당신의 while 루프 루프

이 안에 있습니다. 이것은 for 루프가 완료 될 때까지 계속 묻고, while 루프 상태로 돌아올 때만 종료한다는 것을 의미합니다.

while((continueSurvey != 0) || (i < 3)) { 
    ... 
} 

& & (과) 연산자는 두 조건이 모두 가지고 있음을 상징한다 : 당신도 continueSurvey 0 또는 I 인 경우 = 3 을 루프를 종료하려면

+0

감사합니다. 나는 내 코드에 break 문을 넣는 것을 끝내었다. 나는 for 루프 밖에서 질문을 움직이려고 시도했지만 다음 응답자가 생성되기 전에 질문을 할 필요가있었습니다. –

+1

@MichelleAnderson 그래, 그게 아마도 가장 의미가 솔루션입니다 :) StormeHawke의 대답을 받아 들일 그것은 비슷한 문제가있을 수 있습니다 다른 사람들을 도울 것입니다. – Jordan

-1

이 같은 while 루프를 작성해야 루프가 그 중 하나가 아닌 (|| 또는) 종료하지 않게하려면 참이어야합니다.

+1

아니요 continueSurvey가 0이고 i = 2 인 경우 조건은 true로 평가됩니다. 즉 while 루프를 종료하지 않습니다. &&는 둘 중 하나라도 거짓이면 루프를 종료합니다. – Jordan

2

for 루프 안에 break을 추가해야합니다. IE는

if(continueSurvey == 0) 
    break; 

이것을 for 루프를 종료하고 while 루프를 종료 할 수 있습니다. while 루프에서

+1

고마워요! @StormeHawke –

+0

당신을 환영합니다! 내 대답이 문제를 해결 한 경우 수락 된 답변으로 표시하는 것이 좋습니다 (번호 아래 왼쪽의 확인 표시) – StormeHawke

0

내 조건은 :

((continueSurvey != 0) && (i < 3)) 

while 루프의 내부 블록 경우에만 continuSurvey 경우 실행되는 것을 의미 = 0과 동일한 시간 내가 < 3,2. 조건이 다른 내부 루프가 있습니다. 디버거를 사용하여 내부 루프의 문제를 검색합니다. 이 대답으로는 충분하지 않을 경우 달성하고자하는 것을 지정하십시오.

관련 문제