重置开关案例的值,以便它们可以再次输入菜单。 Java

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

Reset switch case value so they can enter menu input again Java

问题

我正在使用一个开关语句,它之前运行得很好。但是如果在开关语句内部发生异常之类的情况,我希望程序能够返回并要求用户重新选择。

我将分享一些示例代码:

while (valid != true)
{
    System.out.println("输入选项\n1. 选择1\n2. 选择2\n3. 选择3");
    int choice = getChoice();      //获取用户输入的整数选择
    
    switch (choice)
    {
        case 1:
            //执行某些操作
            valid = true;
            break;

        case 2:
            //执行某些操作
            valid = true;
            break;

        case 3:
            //执行某些操作
            valid = true;
            break;

        default:
            System.out.println("无效的选择。");
    }
}

因此,如果出现问题,validChoice 没有设置为 true。我希望用户能够重新输入选择,但由于选择已经设置。它将立即进入 case 3。

我尝试过的一些方法包括:

所有这些都是在出现问题时执行的。

将 choice 设置为 null(希望这会起作用),但是 Java 不允许这样做。

将 choice 设置为 0 只会使其进入默认的 case。

英文:

I am using a switch statement which worked fine. But if something like an exception occurs inside the switch I want the program to return to ask for the users selection again.

I'll share some example code:

while (valid != true)
{
    System.out.println("Enter a Choice \n1. Choice 1. \n2. Choice 2. \n3. Choice 3.");
    int choice = getChoice();      //Get choice just returns an integer from user input.
    
    switch (choice)
    {
        case 1:
            //Do something
            valid = true;
            break;

        case 2:
            //Do something
            valid = true;
            break;

        case 3:
            //Do something
            valid = true;
            break;

        default:
            System.out.println("Invalid Choice.");
    }

So if something went wrong and validChoice wasn't set to true. I want the user to be able to re-enter the choice but since the choice is already set. It will go to case 3 straight away.

Some things ive tried include:

All of these would be done if something goes wrong.

Setting choice to null for if it goes wrong. (Was hopeful that this would work) but java doesn't allow

Setting choice to 0 would just make it go to the default case.

答案1

得分: 1

尝试这个:

do {
    System.out.println("输入选项:\n1. 选项 1.\n2. 选项 2.\n3. 选项 3.");
    int choice = getChoice();  // getChoice() 从用户输入中获取一个整数。

    switch (choice) {
        case 1:
            // 做一些事情
            valid = true;
            break;

        case 2:
            // 做一些事情
            valid = true;
            break;

        case 3:
            // 做一些事情
            valid = true;
            break;

        default:
            System.out.println("无效的选项。");
            break;
    }
} while (valid != true);
}

请注意,我只翻译了您提供的代码部分,其他内容不在翻译范围内。

英文:

Try this :

            do {
                 System.out.println("Enter a Choice \n1. Choice 1. \n2. Choice 2. \n3. Choice 3.");
    int choice = getChoice();      //Get choice just returns an integer from user input.

    switch (choice)
    {
        case 1:
            //Do something
            valid = true;
            break;

        case 2:
            //Do something
            valid = true;
            break;

        case 3:
            //Do something
            valid = true;
            break;

        default:
            System.out.println("Invalid Choice.");
            break;
    }

            } while (valid != true);
}
}

huangapple
  • 本文由 发表于 2020年5月29日 21:37:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/62087286.html
匿名

发表评论

匿名网友

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

确定