抛出异常后如何继续循环?

时间:2013-11-19 05:08:14

标签: loops exception while-loop

所以这就是我所拥有的。我正在研究文本游戏的超级开端,我希望用户四处走动直到他退出。我试图让它抛出异常告诉他他的输入无效,并继续循环。不知道该怎么做。任何帮助表示赞赏!

import java.util.Scanner;

public class Interface {

public static void main(String[] args) {

    String direction;
    System.out.println ("Welcome to the Land of Poonigeddon!");
    System.out.println ("To move, type one of these four letters: N,S,E,W. or type quit to quit.");
    System.out.println ("This will move you in the corresponding cardinal direction.");
    Scanner input = new Scanner (System.in);
    System.out.println ("");
    System.out.println ("Now, which way will you go?");
    while (true) {
        direction = input.nextLine();

           if (direction.equalsIgnoreCase ("e")) {
               System.out.println ("You have moved to the east.");
           }
           if (direction.equalsIgnoreCase("w")) {
               System.out.println("You have moved to the west.");
           }
           if (direction.equalsIgnoreCase("n")) {
               System.out.println ("You have moved to the north.");
           }
           if (direction.equalsIgnoreCase("s")) {
               System.out.print("You have moved to the south.");
           }
           if (direction.equalsIgnoreCase("quit")) {
               break;
           }
           else //throw new exception? i had illegalargumentexception but     it kicks out
               // of the loop after it catches input that isnt     n,w,e,s,quit("Not a valid movement key, please try again.");


    }
}

}

3 个答案:

答案 0 :(得分:1)

在您的情况下,您不需要抓住任何东西。只需在循环中添加System.out.println(question)即可。而对于esle,您的错误消息。

此外,您应该考虑使用else if而不是一堆if

while (true) {
       System.out.println ("Now, which way will you go?");
       direction = input.nextLine();

       if (direction.equalsIgnoreCase ("e")) {
           System.out.println ("You have moved to the east.");
       }
       else if (direction.equalsIgnoreCase("w")) {
           System.out.println("You have moved to the west.");
       }
       else if (direction.equalsIgnoreCase("n")) {
           System.out.println ("You have moved to the north.");
       }
       else if (direction.equalsIgnoreCase("s")) {
           System.out.print("You have moved to the south.");
       }
       else if (direction.equalsIgnoreCase("quit")) {
           break;
       }
       else {
           System.out.println("Not a valid movement key, please try again.")
       }
}

答案 1 :(得分:0)

在这种情况下,为什么要使用例外?只需在循环内输出一条消息。异常应该用于程序运行期间可能发生的异常情况。

while (true) {
    direction = input.nextLine();

       if (direction.equalsIgnoreCase ("e")) {
           System.out.println ("You have moved to the east.");
       }
       if (direction.equalsIgnoreCase("w")) {
           System.out.println("You have moved to the west.");
       }
       if (direction.equalsIgnoreCase("n")) {
           System.out.println ("You have moved to the north.");
       }
       if (direction.equalsIgnoreCase("s")) {
           System.out.print("You have moved to the south.");
       }
       if (direction.equalsIgnoreCase("quit")) {
           break;
       }
       else
           System.out.print("Not a valid movement key, please try again.");
}

答案 2 :(得分:0)

这对我有用,请记住,如果您不想终止程序,则需要使用try catch。在这种情况下,用System.out.println()替换“throw”更有意义。另外不要忘记if块中的continue语句,否则你可能会遇到属于最后一个if的末尾的else。

import java.util.Scanner;

 public static void main(String[] args) {
    System.out.println ("Welcome to the Land of Poonigeddon!");
    System.out.println ("To move, type one of these four letters: N,S,E,W. or type quit to quit.");
    System.out.println ("This will move you in the corresponding cardinal direction.");
    Scanner input = new Scanner (System.in);
    System.out.println ("");
    Boolean playing = true;
    while (playing == true) {
        System.out.println ("Now, which way will you go?"); /*Can go here or in main*/
        String direction = input.nextLine();
        if (direction.equalsIgnoreCase ("e")) {
           System.out.println("You have moved to the east.");
           continue;
        }
        if (direction.equalsIgnoreCase("w")) {
           System.out.println("You have moved to the west.");
           continue;
        }
        if (direction.equalsIgnoreCase("n")) {
           System.out.println("You have moved to the north.");
           continue;
        }
        if (direction.equalsIgnoreCase("s")) {
           System.out.println("You have moved to the south.");
           continue;
        }            
        if (direction.equalsIgnoreCase("quit")) {
           playing = false;
           continue; #these two lines or break work fine
        }
        else {
            System.out.println("Not a valid movement key, please try again.");
        }
    }
}