如何防止用户输入无效答案。

huangapple go评论70阅读模式
英文:

How to prevent a user from entering a invalid answer

问题

我已经尝试阻止用户输入除了"yes"或"no"之外的任何内容,但当他们这样做时,它会一直显示错误,请再试一次。

public static void main(String[] args) {
    System.out.println("你想要添加一个收据吗?是或否");
    Scanner target = new Scanner(System.in);
    String TempAnswer;
    TempAnswer = target.nextLine();
    Boolean flag = true;
    while (flag == true) {
        if ("是".equalsIgnoreCase(TempAnswer)) {
            System.out.println("是");
            flag = false;
        } else if ("否".equalsIgnoreCase(TempAnswer)) {
            System.out.println("否");
            flag = false;
        } else {
            System.out.println("错误,请重新输入!!");
        }
    }
}
英文:

I have tried to stop the user from entering anything besides yes or no however when they do it just keeps on saying error try again forever.

public static void main(String[] args) {
    System.out.println
            ("Would you like to add a Reciept. Yes or No");
    Scanner target = new Scanner(System.in);
    String TempAnswer;
    TempAnswer = target.nextLine();
    Boolean flag = true;
    while (flag == true) {
        if ("yes".equalsIgnoreCase(TempAnswer)) {
            System.out.println("Yes");
            flag = false;
        } else if ("no".equalsIgnoreCase(TempAnswer)) {
            System.out.println("No");
            flag = false;
        } else {
            System.out.println("Error Enter Again!!");
        }
    }
}

答案1

得分: 1

你在 while 循环中从未将输入值重新分配给你的 TempAnswer 变量,导致循环无限运行,因为你从未给予布尔值根据用户输入进行更改的机会。

一些附注:

  1. 使用 Java 命名约定,用 camelCase 定义你的成员变量标识符,而不是 PascalCase
  2. 不要引入多余的代码。使用 while(true) 替代定义布尔变量 flag 并将其用作条件;相应地,使用 break; 替代将 flag 设置为 false。

最终,我会将你的代码重写如下:

public static void main(String[] args) {
    System.out.println("Would you like to add a Reciept. Yes or No");
    Scanner target = new Scanner(System.in);
    String tempAnswer = "";
    while (true) {
        if ("yes".equalsIgnoreCase(tempAnswer)) {
            System.out.println("Yes");
            break;
        } else if ("no".equalsIgnoreCase(tempAnswer)) {
            System.out.println("No");
            break;
        } else {
            System.out.println("Error Enter Again!!");
            tempAnswer = target.nextLine();
        }
    }
}
英文:

You're never re-assigning the input value to your TempAnswer variable in the while loop and loop runs infinitely, as you never give the chance to the boolean value to be changed, based on user's input.

Few side-notes:

  1. Use Java Naming Conventions and define your member variable identifiers with camelCase, not with PascalCase;
  2. Do not introduce redundant code. Use while(true) instead of defining boolean variable flag and using it as the condition; correspondingly, use break; instead of setting flag to false.

Eventually, I would have rewritten your code as follows:

public static void main(String[] args) {
    System.out.println("Would you like to add a Reciept. Yes or No");
    Scanner target = new Scanner(System.in);
    String tempAnswer = "";
    while (true) {
        if ("yes".equalsIgnoreCase(tempAnswer)) {
            System.out.println("Yes");
            break;
        } else if ("no".equalsIgnoreCase(tempAnswer)) {
            System.out.println("No");
            break;
        } else {
            System.out.println("Error Enter Again!!");
            tempAnswer = target.nextLine();
        }
    }
}

答案2

得分: 1

public static void main(String[] args) {
    System.out.println("Would you like to add a Receipt. Yes or No");
    Scanner target = new Scanner(System.in);
    while (true) {
        String tempAnswer = target.nextLine();
        if ("yes".equalsIgnoreCase(tempAnswer)) {
            System.out.println("Yes");
            break;
        }
        if ("no".equalsIgnoreCase(tempAnswer)) {
            System.out.println("No");
            break;
        }
        System.out.println("Error Enter Again!!");
    }
}
英文:

Get rid of the superfluous calls and make sure that you read the answer in the loop. If you don't do that, you'll indefinitely loop because you're not changing the loop-condition.

Use break to break the loop instead of using flags for such small scope.

Also, don't use Boolean, use boolean instead. You will probably never have to use Boolean, so don't use it.

public static void main(String[] args) {
    System.out.println("Would you like to add a Reciept. Yes or No");
    Scanner target = new Scanner(System.in);
    while (true) {
        String tempAnswer = target.nextLine();
        if ("yes".equalsIgnoreCase(tempAnswer)) {
            System.out.println("Yes");
            break;
        }
        if ("no".equalsIgnoreCase(tempAnswer)) {
            System.out.println("No");
            break;
        }
        System.out.println("Error Enter Again!!");
    }
}

答案3

得分: -1

尝试在输入正确答案后终止循环

public static void main(String[] args) {
    System.out.println("Would you like to add a Receipt. Yes or No");
    Scanner target = new Scanner(System.in);
    String TempAnswer;
    TempAnswer = target.nextLine();
    while (true) {
        if ("yes".equalsIgnoreCase(TempAnswer)) {
            System.out.println("Yes");
            break;
        } else if ("no".equalsIgnoreCase(TempAnswer)) {
            System.out.println("No");
            break;
        } else {
            System.out.println("Error Enter Again!!");
        }
    }
}
英文:

Try to break the loop when the correct answer is typed

public static void main(String[] args) {
    System.out.println
        ("Would you like to add a Reciept. Yes or No");
    Scanner target = new Scanner(System.in);
    String TempAnswer;
    TempAnswer = target.nextLine();
    while (True) {
        if ("yes".equalsIgnoreCase(TempAnswer)) {
            System.out.println("Yes");
            break;
        } else if ("no".equalsIgnoreCase(TempAnswer)) {
            System.out.println("No");
            break;
        } else {
            System.out.println("Error Enter Again!!");
        }
    }
}

huangapple
  • 本文由 发表于 2020年9月18日 18:00:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63953564.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定