在try / catch语句中循环程序

时间:2013-10-10 01:02:21

标签: java

我有一个程序,在第一个实例中告诉用户他们的选项并接受与他们预先选择的选项相对应的用户输入。我有异常处理,但如果有异常我如何让程序保持循环,直到接受有效的输入?

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

    }
}

3 个答案:

答案 0 :(得分: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;
         }           
    }


}

答案 1 :(得分: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");
        }
    }
}

答案 2 :(得分:-1)

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

相关问题