Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 抛出异常后如何继续循环?_Loops_Exception_While Loop - Fatal编程技术网

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

Loops 抛出异常后如何继续循环?,loops,exception,while-loop,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 P

这就是我所拥有的。我正在制作一个文本游戏的超级开始,我希望用户在退出之前四处走动。我试图让它抛出一个异常,告诉他他的输入无效,然后继续循环。不知道怎么做。感谢您的帮助

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.");


    }
}

}在你的情况下,你不需要捕捉任何东西。只需在循环中添加
System.out.println(问题)
。对于esle,您的错误消息

也应考虑使用<代码>否则,如果而不是一组<代码> >

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.")
       }
}

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

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.");
}

这对我来说很有效,请记住,如果您不想终止程序,您可能会希望使用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.");
        }
    }
}