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 ループを追加するだけで、入力が正しいと確信できる場合は「終了」ブール値を true に設定するか、例外がキャッチされた場合は「終了」ブール値を 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に入れ、エラー メッセージを出力した後にそのメソッドを呼び出すか、コメントで peeskilet が言ったことを実行します。

于 2013-10-10T01:05:09.523 に答える