1

while ステートメントが機能しない理由を誰かが理解するのを手伝ってくれますか? ループは i = 3 の後に停止しますが、continueSurvey = 0 の場合は停止しません。continueSurvey を O に変更すると実行されますが、ループは終了しません。プロセスにステップインして、変数が0、ループは継続します。

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++;
                }
            } 
    }
}
4

4 に答える 4

0

while ループの条件は次のとおりです。

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

これは、同時に continuSurvey != 0 かつ i < 3 の場合に限り、while ループの内部ブロックが実行されることを意味します。条件が異なる内部ループがあります。デバッガーを使用して内部ループの問題を検索します。この回答で十分でない場合は、達成したいことを具体的に記入してください。

于 2013-08-06T17:44:15.583 に答える