2

私はJavaが初めてで、これは私が開発した最初のプログラムです。基本的に、「1」を入力すると、ゲームに入ります。

レベルを 1 に設定します。

次に、レベルに基づいて問題を選択します。最初のラウンドは問題なく動作します。ラウンド 1 で正解した後、質問 2 に移動します。

しかし、問題 2 では、私が正しく答えると、次の問題に移動する代わりに、問題 2 に固執して繰り返します。

なぜそれをしているのですか?私はレベル++を持っていますか?

    public static void main (String[] args) {
        gameRoom();
    }

    public static void gameRoom() {
        System.out.println("Welcome to the Truth and False game!");
        System.out.println("Please write 1 to start, or -1 to exit program.");
        InputI = console.nextInt();

        if(InputI == 1) {
            inGame = true;
            gameProcess(1);
        }
    }

    public static void gameProcess(int level) {

        switch (level) {

            case 1:
                question = tof.getQuestion(1);
                System.out.println("Your question is: " + question);
                System.out.println("Please answer, true or false!.");
                level = 1;
            break;

            case 2:
                question = tof.getQuestion(2);
                System.out.println("Your question is: " + question);
                System.out.println("Please answer, true or false!.");   
                level = 2;
            break;

            case 3:
                System.out.println("Hey1");
            break;
        }

        while (inGame) {
            InputS = console.nextLine();
            while (InputS != "") {
                inGame = false;                 
                InputS = console.nextLine();
                checkQuestion(level, InputS);               
            }
        }
    }

    public static void checkQuestion (int level, String question)
    {
        Boolanswer = tof.checkQuestion(level, question);
        level++;
        if (Boolanswer) {
            System.out.println("Correct!" + correctAnswers);
            correctAnswers++;
        } else {
            System.out.println("Incorrect! " + correctAnswers);             
        }

        inGame = true;
        gameProcess(level);
    }

問題は、システムが input != "" のときにループを読み取れないことかもしれません。間違っているかもしれませんが、これは他のクラスです。

    private String[][] questionsArray = new String[][]
    {
        {"Is sky blue?", "true"},
        {"Is sky green?", "false"}
    };

    private String answer = "";
    private String Currentanswer = "";
    private String question = "";
    private int level = 0;
    private boolean returnValue = false;

    public String getQuestion(int Level) {
        switch (Level) {

        case 1:
            question = questionsArray[0][0];
        break;

        case 2:
            question = questionsArray[1][0];
        break;      
        }

        return question;
    }

    public String getAnswer(int Level) {
        switch (Level) {

            case 1:
                answer = questionsArray[0][1];
            break;

            case 2:
                answer = questionsArray[1][1];
            break;          
        }

        return answer;
    }   

    public boolean checkQuestion(int level, String answer) {

        switch (level) {
            case 1:
                Currentanswer = questionsArray[0][1];
                if (Currentanswer.equalsIgnoreCase(answer)) {
                    returnValue = true;
                } else {
                    returnValue = false;
                }
            break;

            case 2:
                Currentanswer = questionsArray[1][1];
                if (Currentanswer.equalsIgnoreCase(answer)) {
                    returnValue = true;
                } else {
                    returnValue = false;
                }
            break;              
        }
        return returnValue;
    }

問題は、2 番目の質問に正しく答えると、何も出力されないことです。不正解の場合は、最初の質問に移動します。

質問は 2 つしかありませんが、テストしているだけです。レベルが 3 になり、"Hey!" と表示されます。スイッチケースによると。

4

2 に答える 2