做循环验证并重试。

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

do while loop validation and try again

问题

我在Java方面相对新手,想知道如何在do...while循环中强制用户输入有效选项?

我想要实现的目标是,只要用户没有选择退出选项(在我的情况下是'4',它是一个字符而不是整数),就显示一个菜单。但是,如果选择无效,我想要显示错误消息并提示用户进行新的选择,这一次不再显示菜单。

到目前为止,在我的do...while循环内,我根据用户的选择显示不同的信息。如果他们输入的是1-4以外的任何内容,他们最终会进入显示错误消息的最后一个else if分支,但与此同时,他们会离开if/else if循环,然后回到主菜单。

不知道这样是否清楚,但任何帮助都将不胜感激。我还尝试过使用switch来做同样的事情,但是遇到了相同的问题。

谢谢。

do {
    // 显示主菜单

    if (menu == '1') { ... }
    else if (menu == '2') { ... }
    else if (menu == '3') { ... }
    else if (menu == '4') { ... }
    else if (menu != '4') { // 显示错误消息 }
} while (menu != '4');
英文:

I'm fairly new to java and was wondering how would one force a user to enter a valid option in a do...while loop?
What i'm trying to achieve here is to display a menu for as long as the user does not select the exit option which is '4' in my case (it's a char not an int). But if the selection is invalid I want to display an error message and prompt the user to make a new selection, this time, without displaying the menu again.

So far, inside my do...while loop i'm displaying different information according to the user selection. If they enter anything other than 1-4, they end up in my last else if which displays an error message, but at the same time they leave the if/else if loop and end up back in the main menu.

Don't know if this is clear but any help would be appreciated. I also tried the same thing with switch but got the same problem.

Thanks.

do { 
    // display main menu

    if (menu == '1') { ... }
    else if (menu == '2') { ... }
    else if (menu == '3') { ... }
    else if (menu == '4') { ... }
    else if (menu != '4') { // display error message }
} while (menu != '4')

答案1

得分: 1

好的,下面是翻译好的内容:


好的,你有一个工作,我们可以用一个自包含的方式来描述它,通过一个简短的明确参数列表:

  • 要求用户输入。
  • 检查输入是否有效。
  • 如果无效,继续要求。

就是这样。这是一个我们可以编写的工作。而且很容易。

所以,做到这一点!创建一个方法来执行这个工作。你只需要一个参数,那就是:“什么样的输入算是有效”。如果我们可以简化,只需要一个字符,从 '1' 到这个字符的所有内容都是有效的,那么:

public char askUser(char maxValid) {
    do {
        char in = askUserForInput(); // 无论你怎么得到那个字符。
        if (in >= &#39;1&#39; || in <= maxValid) return in;
        System.out.println("请输入一个介于1和" + maxValid + "之间的值 > ");
    } while (true);
}

当你需要输入时,你可以直接调用这个方法。

你可以将这个逻辑(也就是一个内嵌在你的主要循环中的 do/while 循环)整合到主循环中,但编写优秀代码的两个重要方面是找到易于隔离的方面,并将它们隔离出来(可以是创建新方法、类型、模块或子系统 - 这适用于整个层次结构),以及避免重复自己。

英文:

Okay, so you have a job which we can describe in a self-contained fashion with a short list of clearly stated parameters:

  • Ask the user for input.
  • Check that the input is valid.
  • If not, keep asking.

That's it. That's a job we can write. Easily at that.

So, do that! Make a method to do just this job. There's only one parameter you need, which is: "What constitutes valid input". If we can simplify that we just need a character, and everything from &#39;1&#39; to this char is valid, then:

public char askUser(char maxValid) {
    do {
        char in = askUserForInput(); // however you get that char.
        if (in &gt;= &#39;1&#39; || in &lt;= maxValid) return in;
        System.out.println(&quot;Enter a value between 1 and &quot; + maxValid + &quot;&gt; &quot;);
    } while (true);
}

Then you can just call this method when you need input.

You can roll this logic (so, that'd be a do/while loop inside your do/while loop) into the main loop, but two rather significant aspects of writing good code is to find easily isolatable aspects and to, well, isolate them (be it making new methods, types, modules, or subsystems - it applies across the entire hierarchy), and to avoid repeating yourself.

huangapple
  • 本文由 发表于 2020年10月24日 10:17:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/64509328.html
匿名

发表评论

匿名网友

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

确定