我希望能够处理用户输入除 Int 之外的任何内容的异常,以便它会引发稍后可以处理的异常,并向用户显示一条用户友好的消息,建议输入一个 int。
final int FEES=3000;
String [] user = new String [2];
String [] password = new String [3];
int userChoice, sdev, maths, systemAnalysis, cmpArch, menu, studentFees=0, grantFees=0, hundred=0, seventyFive=0, fifty=0, noGrant=0, grantAwarded=0, processed=0, totalStudentFees=0;
double average;
String studentName, studentNumber;
Scanner in=new Scanner(System.in);
System.out.println("\t\tPlease create your College Grant account");
System.out.print("Please enter your desired username: ");
user[0]=in.next();
System.out.print("\nPlease enter the password for your account: ");
password[0]=in.next();
System.out.println("\n\n\t\tSecurity Feature");
System.out.print("Please enter your username: ");
user[1]=in.next().toLowerCase();
System.out.print("\nPlease enter your password: ");
password[1]=in.next();
System.out.print("\nPlease re-enter your password: ");
password[2]=in.next();
if(user[1].equals(user[0]) && password[1].equals(password[0]) && password[2].equals(password[0])){
System.out.println("\nPassword has been verified successfully.");
System.out.println("\n\n\t\t College Grant System");
System.out.println("1.\t Calculate Grant");
System.out.println("2.\t Fee Statistics");
System.out.println("3.\t Grant Category Information");
System.out.println("4.\t Exit");
System.out.print("\nEnter your choice: ");
userChoice=in.nextInt();
while (userChoice!=1 && userChoice!=2 && userChoice!=3 && userChoice!=4){
System.out.println("\t Inavlid Choice");
System.out.println("\n\t\t College Grant System");
System.out.println("1.\t Calculate Grant");
System.out.println("2.\t Fee Statistics");
System.out.println("3.\t Grant Category Information");
System.out.println("4.\t Exit");
System.out.print("\nEnter your choice: ");
userChoice = in.nextInt();
}