字符串输入的Java用户验证,直到给出正确的输入为止 [英] java user validation for string input until correct input is given

查看:38
本文介绍了字符串输入的Java用户验证,直到给出正确的输入为止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何使用while循环不断要求用户重复输入有效答案,直到给出有效输入,并在给出有效输入时结束程序?我只知道如何使用 int 和数字.我被信件弄糊涂了.我应该如何应用NOT运算符或其他逻辑运算符 ||&& .

How would I use a while loop to keep asking the user to type a valid answer repeatedly until a valid input is given and end the program when a valid input is given? I only know how to use int and numbers. I am confused by letters. How should I apply the NOT operator or other logical operators || &&.

Scanner myScan = new Scanner(System.in);                                        
System.out.println("Enter food");                                       
String food = myScan.nextLine();

if (food.equalsIgnoreCase("b"))
{
    System.out.println("beans");
}
else if (food.equalsIgnoreCase("e"))                                        
{
    System.out.println("eggs");
}
else
{
    System.out.println("error");
}

推荐答案

在一个非常简单的级别上,我将使用 do-while 循环,因为您希望至少进入一次循环.然后,我将使用 boolean 标志确定输入的有效性,然后基于该标志做出进一步的确定,例如...

At a very simple level, I'd use a do-while loop, as you want to enter the loop at least once. I'd then determine the validity of the input, using a boolean flag and make further determinations based on that, for example...

Scanner myScan = new Scanner(System.in);
boolean userInputCorrect = false;
String food = null;
do {
    System.out.println("Enter food");
    food = myScan.nextLine();
    userInputCorrect = food.equalsIgnoreCase("b") || food.equalsIgnoreCase("e") || food.equalsIgnoreCase("exit");
    if (!userInputCorrect) {
        System.out.println("Error");
    }
} while (!userInputCorrect);
System.out.println("You selected " + food);

扩展的解决方案可能使用某种 valid 方法,我可以在其中传递 String 并让其根据已知值验证输入,但这可能是一个几乎没有问题的范围

An expanded solution might use some kind of valid method, into which I can pass the String and have it validate the input based on known values, but that's probably a little beyond the scope of the question

正如已经正确指出的,但其他人指出,将输入一次转换为小写并比较所有值会更有效,在这种情况下,最好使用 switch 声明...

As has been, correctly, pointed out but others, it would be more efficient to convert the input to lowercase once and compare all the values, in this case, it might be better to use a switch statement...

Scanner myScan = new Scanner(System.in);
boolean userInputCorrect = false;
String food = null;
do {
    System.out.println("Enter food");
    food = myScan.nextLine();
    switch (food.toLowerCase()) {
        case "b":
        case "e":
        case "exit":
            userInputCorrect = true;
            break;
        default:
            System.out.println("Error");
    }
} while (!userInputCorrect);
System.out.println("You selected " + food);

但是你也可以做...

But you could also do...

food = myScan.nextLine().toLowerCase();
userInputCorrect = food.equals("b") || food.equals("e") || food.equals("exit");

这篇关于字符串输入的Java用户验证,直到给出正确的输入为止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆