[英]Scanner input with exception handling in a try/catch is not working like intended
我正在制作一個接受帳號和密碼的 atm 程序。 當我嘗試輸入除接受的帳戶和預先確定的 pin 之外的任何內容時,它應該會給出一條錯誤消息,然后用戶再試一次。 唯一的問題是它會打印出錯誤消息,但會轉到下一個輸入而不是重復上一個輸入。 即從輸入帳號到輸入密碼。 同樣的事情也發生在引腳上。 我真的不知道該怎么辦。 提前致謝! 這是我的代碼片段:
import java.util.*;
import java.lang.*;
public class Menu {
Scanner input = new Scanner(System.in);
//Bank bank = new Bank();
boolean keepGoing = true;
public static void main(String[] args){
Menu menu = new Menu();
menu.startMenu();
}//end main
public void startMenu(){
int choice = 0;
int verify1 = 0;
int verify2 = 0;
int account = 0;
int pin = 0;
printGreet();
while(keepGoing){
System.out.print("Please enter Account number: ");
do{
try{
account = input.nextInt();
}//end try
catch(NumberFormatException e){
System.out.print("ERROR: That is not a valid account number. Please enter a valid account number (#####): ");
input.nextLine();//clear buffer
}//end catch
catch(InputMismatchException e){
System.out.print("Error: That is not a valid account number. Please enter a valid account number (#####): ");
input.nextLine();//clear buffer
}//end catch
if(account < 0 || account > 99999){
System.out.print("ERROR: That is not a valid account number. Please enter a valid account number (#####): ");
//input.nextLine();//clear buffer
}//end if
}while(!(account >= 0 && account <= 99999));//end do/while
System.out.print("Please enter PIN number: ");
do{
try{
pin = input.nextInt();
}//end try
catch(NumberFormatException e){
System.out.print("ERROR: That is not a valid pin number. Please enter a valid pin number (#####): ");
input.nextLine();//clear buffer
}//end catch
catch(InputMismatchException e){
System.out.print("ERROR: That is not a valid pin number. Please enter a valid pin number (#####): ");
input.nextLine();//clear buffer
}//end catch
if(pin < 0 || pin > 99999){
System.out.print("ERROR: That is not a valid pin number. Please enter a valid pin number (#####): ");
//input.nextLine();//clear buffer
}//end if
}while(!(pin >= 0 && pin <= 99999));//end do/while
verify1 = verifyAccount(account);
verify2 = verifyPin(pin);
if((verify1 == 1) && (verify2 == 1)){
printAdminMenu();
choice = getAdminChoice();
adminChoices(choice);
}else if((verify1 == 2) && (verify2 == 2)){
printUserMenu();
choice = getUserChoice();
userChoices(choice);
}else{
System.out.println("ERROR: YOU ARE NOT AN AUTHORIZED USER...GOODBYE");
System.exit(0);
}//end if/else
}//end while
}//end startMenu()
whiles 只檢查帳號是否無效。 當發生解析錯誤時, account
/ pin
變量仍為0
因此不會重復。
我為您修復/改進了代碼。 請注意,在您在 StackOverflow 上提問之前,您應該始終調試您的代碼。
public void startMenu() {
int account = 0;
int pin = 0;
printGreet();
while (keepGoing) {
System.out.print("Please enter Account number: ");
boolean invalid;
do {
invalid = false;
try {
account = input.nextInt();
// Cancel if the number is invalid
if (account < 0 || account > 99999) {
throw new NumberFormatException();
}
} catch (NumberFormatException | InputMismatchException e) {
System.out.print("ERROR: That is not a valid account number. Please enter a valid account number (#####): ");
input.nextLine();
// Mark the loop iteration as invalid
invalid = true;
}
} while (invalid);
System.out.print("Please enter PIN number: ");
do {
invalid = false;
try {
pin = input.nextInt();
// Cancel if the number is invalid
if (pin < 0 || pin > 99999) {
throw new NumberFormatException();
}
} catch (NumberFormatException | InputMismatchException e) {
System.out.print("ERROR: That is not a valid pin number. Please enter a valid pin number (#####): ");
input.nextLine();
invalid = true;
}
} while (invalid);
verify1 = verifyAccount(account);
verify2 = verifyPin(pin);
if ((verify1 == 1) && (verify2 == 1)) {
printAdminMenu();
choice = getAdminChoice();
adminChoices(choice);
} else if ((verify1 == 2) && (verify2 == 2)) {
printUserMenu();
choice = getUserChoice();
userChoices(choice);
} else {
System.out.println("ERROR: YOU ARE NOT AN AUTHORIZED USER...GOODBYE");
System.exit(0);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.