0

だから私はユーザーにブール値として答えを入力させてからエラーチェックをさせようとしていますが、それはうまくいかないようです。私は正しい軌道に乗っていますか、それともこれを行うためのより良い方法はありますか? 入力が文字列である場合にそれを実行し、「true」と「false」に割り当てた他の 2 つの文字列に対してチェックすることを考えていましたが、while ループ:

while (!continueGame.equalsIgnoreCase(answerTrue) && !continueGame.equalsIgnoreCase(answerFalse)) {    
        System.out.println("Would you like to try again? (true/false)");
        continueGame = keyboard.nextBoolean();
        System.out.println();
    }

どちらも機能しませんでした。私はそれが私のnotのものと関係があることはかなり確信していますが、その理由はわかりません. とにかく、以下は文字列の代わりにブール値を使用したエラーチェックの方法です。文字列バージョンも基本的には同じですが、文字列用に変更しただけです。

public static boolean continueGame() {
    boolean continueGame;

    System.out.println("Would you like to try again? (true/false)\n");
    continueGame = keyboard.nextBoolean();
    System.out.println();

    while (continueGame != true && continueGame != false) {    
        System.out.println("Would you like to try again? (true/false)");
        continueGame = keyboard.nextBoolean();
        System.out.println();
    }
    if (continueGame) {
        return true;
    }
    else {
        System.out.println("We accept your surrender.");
        return false;
    }

} //End continueGame method
4

2 に答える 2