How to repeat an if or switch statement if something out of option is entered?

|▌冷眼眸甩不掉的悲伤 提交于 2020-07-15 08:13:54

问题


This will mostly get duplicated but I sincerely don't know how to search this question since it is too long and complicated. Sorry in advance!

Back to the problem, let's say I take an input from the user with the Scanner class. Imagine that Scanner is imported and everything is set.

Scanner scan = new Scanner();
String input = scan.nextLine();
switch( input) {
    case "attack":
       System.out.println( "You attacked the enemy!");
       break;

    case "defend":
       System.out.println( "You blocked the enemy!");
       break;

    default:
       System.out.println( "This is not an option!");
       // somehow repeat the process until one of the case options are 
       // entered.
}

How do I repeat this process of asking for an input and checking for a case until a case is executed?

I could put this inside a while loop and when a case option is entered I could exit out of the while loop, but that just seems too much code when I have plenty of switch/if statements that both require a solid input in order to process the rest of the code. Is there a way to do this more efficiently in Java?


回答1:


Scanner scan = new Scanner(System.in);
loop:
while (true) {
    String input = scan.nextLine();
    switch (input) {
        case "attack":
            System.out.println("You attacked the enemy!");
            break loop;

        case "defend":
            System.out.println("You blocked the enemy!");
            break loop;

        default:
            System.out.println("This is not an option!");
            break;
    }
}

while (true) makes an infinite loop as you may know. In the switch statement, if the input is attack or defend, we break out of the loop. If it is neither of those, we only break out of the switch statement.

The while loop is marked with the loop: label. This is so that we can tell it to break loop; to break out of the loop. On the other hand break only breaks out of the switch statement.




回答2:


One possible option would be to create a Map<String, String> with valid options and pass it to a method with an infinite loop, if the user provides a valid option return it (possibly after performing some action); otherwise display the invalid option message and loop again. Like,

public static String getCommand(Scanner scan, Map<String, String> options) {
    while (true) {
        String input = scan.nextLine();
        if (options.containsKey(input)) {
            System.out.println(options.get(input));
            return input;
        } else {
            System.out.printf("%s is not an option!%n", input);
        }
    }
}

The above could then be called like,

Scanner scan = new Scanner(System.in);
Map<String, String> options = new HashMap<>();
options.put("attack", "You attacked the enemy!");
options.put("defend", "You blocked the enemy!");
String cmd = getCommand(scan, options);

After which cmd would be one of attack or defend. Note that this way you can add as many options (or type of options) as you need, and reuse getCommand.




回答3:


So if you enter defend or attack, it will not ask again, otherwise repeat the process. Try this:

    do{
    String input = scan.nextLine();
    switch( input) {
        case "attack":
           System.out.println( "You attacked the enemy!");
           break;

        case "defend":
           System.out.println( "You blocked the enemy!");
           break;

        default:
           System.out.println( "This is not an option!");
           // somehow repeat the process until one of the case options are 
           // entered.
    }
}while(!"attack".equalsIgnoreCase(input) || !"defend".equalsIgnoreCase(input))


来源:https://stackoverflow.com/questions/41318336/how-to-repeat-an-if-or-switch-statement-if-something-out-of-option-is-entered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!