0

我有一个程序,它首先告诉用户他们的选项并接受与他们喜欢的选项相对应的用户输入。我对此有异常处理,但如果有异常,我如何让程序继续循环,直到接受有效输入?

import java.util.InputMismatchException;
import java.util.Scanner;
public class Application 
{

    public static void main(String[] args) 
    {
        try
        {
            int option;
            Scanner input = new Scanner(System.in);
            System.out.println("Welcome to Toys Rental Ltd! \n\nChoose an option: "
                    + "\n-Rent a toy (enter 1) "
                    + "\n-Return a toy (enter 2) "
                    + "\n-Add a new member of a toy (enter 3) "
                    + "\n-Update member details (enter 4) "
                    + "\n-Update toy details (enter 5) "
                    + "\n-Print a member and toy list (enter 6) "
                    + "\n-Print a member and rental statment (enter 7) "
                    + "\n Or for help please enter 0");

            option = input.nextInt();

            if(option 

        }

        catch (InputMismatchException iie)
        {
            System.out.println("Invalid input, please try again");
        }

    }
}
4

3 回答 3

1

好吧,放在一个循环中。

    import java.util.InputMismatchException;

public class Application 
{

public static void main(String[] args) 
{
    boolean retry=true;
    while (retry){
          try
          {

                int option;
                Scanner input = new Scanner(System.in);
                System.out.println("Welcome to Toys Rental Ltd! \n\nChoose an option: "
                + "\n-Rent a toy (enter 1) "
                + "\n-Return a toy (enter 2) "
                + "\n-Add a new member of a toy (enter 3) "
                + "\n-Update member details (enter 4) "
                + "\n-Update toy details (enter 5) "
                + "\n-Print a member and toy list (enter 6) "
                + "\n-Print a member and rental statment (enter 7) "
                + "\n Or for help please enter 0");

                option = input.nextInt();

                retry=false;
         }

         catch (InputMismatchException iie)
         {
             System.out.println("Invalid input, please try again");
              retry=true;
         }           
    }


}
于 2013-10-10T01:09:01.910 回答
0

只需添加while循环,当您确定输入正确时将“end”布尔值设置为true,或者在捕获异常时将“end”布尔值设置为false。

然而在这个例子中,异常不是处理它的好方法,简单的“if (good_input)”就足够了。

public static void main(String[] args) {
    boolean end = false;
    while (end == false) {
        try {
            int option;
            Scanner input = new Scanner(System.in);
            System.out.println("Welcome to Toys Rental Ltd! \n\nChoose an option: "
                    + "\n-Rent a toy (enter 1) "
                    + "\n-Return a toy (enter 2) "
                    + "\n-Add a new member of a toy (enter 3) "
                    + "\n-Update member details (enter 4) "
                    + "\n-Update toy details (enter 5) "
                    + "\n-Print a member and toy list (enter 6) "
                    + "\n-Print a member and rental statment (enter 7) "
                    + "\n Or for help please enter 0");

            option = input.nextInt();

            if (option == 1) {
                //.....
            }
            //.....
            end = true;
        } catch (InputMismatchException iie) {
            end = false;
            System.out.println("Invalid input, please try again");
        }
    }
}
于 2013-10-10T01:13:18.383 回答
-1

将请求和接受输入的逻辑放入除 之外的方法main中,并在打印错误消息后调用该方法,或者执行 peeskillet 在评论中所说的操作。

于 2013-10-10T01:05:09.523 回答