繁体   English   中英

在 try/catch 中进行异常处理的扫描器输入未按预期工作

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM