Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java布尔输入错误检查_Java_Boolean_User Input_Error Checking - Fatal编程技术网

Java布尔输入错误检查

Java布尔输入错误检查,java,boolean,user-input,error-checking,Java,Boolean,User Input,Error Checking,所以我试图让用户以布尔形式输入答案,然后进行错误检查,但这似乎不起作用。我是走对了路还是有更好的方法?我曾想过在输入为字符串的情况下执行此操作,并将其与我分配给“true”和“false”的其他两个字符串进行比较,但我的while循环: while (!continueGame.equalsIgnoreCase(answerTrue) && !continueGame.equalsIgnoreCase(answerFalse)) { System.out.

所以我试图让用户以布尔形式输入答案,然后进行错误检查,但这似乎不起作用。我是走对了路还是有更好的方法?我曾想过在输入为字符串的情况下执行此操作,并将其与我分配给“true”和“false”的其他两个字符串进行比较,但我的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();
    }
也不管用。我相当肯定这与我的“不”有关,但我不确定为什么。无论如何,下面是我使用布尔而不是字符串检查错误的方法。字符串版本基本上是相同的方式,但只是针对字符串进行了修改

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

基本上,我将交换循环的工作方式,这样就可以达到中断条件

import java.*;
import java.util.Scanner;

public class Loop
{
 public static void main(String[] args)
 {
   boolean continueGame=true;
   Scanner keyboard = new Scanner(System.in);
   System.out.println("Would you like to try again? (true/false)\n");
   continueGame = keyboard.nextBoolean();

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

这将无限期地提示用户输入真/假,直到用户输入有效的布尔值。然后它将跳出循环,您可以继续使用其余的代码。

您可以更具体地说明哪些代码不起作用吗?另外,为了让你的代码更加干练,我建议你进行一个后检查循环,并在循环之前移除代码块。你对continueGame做了些奇怪的事!=真的&&continueGame!=false,因为布尔值怎么可能是“not true AND not false?”布尔值要么是true,要么是false,从广义上说,要么是not true,要么是not false。我认为这并不能满足OP的要求。我想如果用户输入了不同的内容,他会一直询问true/false,但我希望他确认。你应该将扫描仪键盘声明在循环之外。每次创建它都是浪费,在某些情况下可能会导致不期望的行为。
while(true) {

    Scanner keyboard = new Scanner(System.in);
    System.out.println("Would you like to try again? (true/false)");

    if(keyboard.hasNextBoolean()){
        continueGame = keyboard.nextBoolean();
        System.out.println();
        break;
    }

}