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