简体   繁体   English

使用布尔值从用户字符串 java 输入验证

[英]Input validation from user strings java using a boolean

I am creating a rock paper scissors game and the only part that I'm hung up on is the input validation.我正在创建一个石头剪刀布游戏,我唯一挂起的部分是输入验证。 I have tried do/while loops, try/catch but just can't seem to get it to function properly.我尝试过 do/while 循环、try/catch 但似乎无法让它正常运行。 Below is what I currently have for the getUserChoice method.下面是我目前拥有的 getUserChoice 方法。 Any help would be greatly appreciated.任何帮助将不胜感激。 Thanks!谢谢!

public String getUserChoice(){
    String user = " ";
    boolean error = false;

    Scanner in = new Scanner(System.in);
    System.out.println("Please enter rock, paper, or scissors: ");
    user = in.nextLine(); //reads user input

    do {
        if (user.equalsIgnoreCase("rock")) {
            user = "rock";
            error = false;
        }
        else if (user.equalsIgnoreCase("paper")) {
            user = "paper";
            error = false;
        }
        else if (user.equalsIgnoreCase("scissors")) {
            user = "scissors";
            error = false;
        }
    }
    while(error); {
        System.out.println("Please enter valid input: ");
    }
    return user;
}

I guess you want input only "rock" , "paper" and "scissors"我猜你只想输入 "rock" , "paper" 和 "scissors"

the do just the只做

if(!input.equals("rock") || !input.equals("paper") || 
            !input.equals("scissors")) System.out.println("invalid input");

or或者
if(!input.matches("rock|paper|scissors")) System.out.println("invalid input");

You can slightly change your code to:您可以将代码稍微更改为:

public String getUserChoice(){
String user = " ";
boolean error = true;

Scanner in = new Scanner(System.in);
System.out.println("Please enter rock, paper, or scissors: ");
user = in.nextLine(); //reads user input

do {
    if (user.equalsIgnoreCase("rock")) {
        user = "rock";
        error = false;
    }
    else if (user.equalsIgnoreCase("paper")) {
        user = "paper";
        error = false;
    }
    else if (user.equalsIgnoreCase("scissors")) {
        user = "scissors";
        error = false;
    } else {
        System.out.println("Please enter valid input: ");
        user = in.nextLine(); 
    }
}
while(error);

return user;
}

boolean error = true; makes the while block to iterate indefinitely, otherwise, It will jump out on the first iteration.使 while 块无限迭代,否则,它会在第一次迭代时跳出。

The else block is what gets executed when not a good condition is input. else块是在输入不好的条件时执行的块。 The system.out outside the while just get executed after the loop finishes while 之外的 system.out 在循环结束后才被执行

A more efficient or readable way to implement this would be to substitute the do-while block by:实现这一点的更有效或可读的方法是将 do-while 块替换为:

while (true){
    if (user.equalsIgnoreCase("rock") || user.equalsIgnoreCase("paper")  || user.equalsIgnoreCase("scissors")){
        return user;
    } else {
        System.out.println("Please enter valid input: ");
        user = in.nextLine(); 
    }       
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM