2

第一次发帖,这里的人似乎很有帮助。我目前正在编写我的第二个简单银行 atm/帐户的任务。我的用户密码登录代码没有执行以下操作。

1.在数组中找不到pin时返回循环开始

2.引脚错误使程序停止

这里只是登录脚本的片段。问题在于while (menuChoice==1){}循环。

public static void main(String args[]) {
    Scanner in = new Scanner(System.in);

    boolean userLogin = false;          //Flag for user login authorization 
    boolean progOn=true;            //confirmation that program is still be running         

    int num = 0, i = 0;
    int Bal[] = {500, 250, 400, 700};
    String Names[] = {"Niall", "Sean", "John", "Connor"};
    String pin[] = {"1234", "2345", "3456", "4567"};
    String menu2 = "\n1. Deposit \n2. Withdrawal \n3. Balance \n4: Change pin \n5: Exit";
    String pinChoice = "", pinChange="";

    int sub = 0;            //To keep the subscript
    String startMenu = "";      //The entered choice for the Start menu as a string
    int menuChoice = 0;     //The entered choice for the menu as an int

    //--------------------
    //login script
    //--------------------
    while (progOn == true) {
        System.out.println("------------------------");
        System.out.println("Welcome! Please select an option...\n");
        System.out.println(">1. Login");
        System.out.println(">2. Quit");
        System.out.println("------------------------");

        startMenu = in.next();

        while (!startMenu.matches("[1-2]")) {       //If the user enters a non digit, give message and return
            System.out.println("\nError. Please enter a valid Menu option.\n");
            System.out.println(">1. Login");
            System.out.println(">2. Quit");

            System.out.print("Please select an option\n");          //Ask the user to choose again
            startMenu = in.next();
        }//end while loop

        menuChoice = Integer.parseInt(startMenu);   //Parse the string to an int

        while (menuChoice == 1) {           //While loop to carry out the users choice
            System.out.println("------------------------");
            System.out.println("Please enter your pin number: ");
            System.out.println("------------------------");
            pinChoice = in.next();

            while (!pinChoice.matches("\\d{4}")) {  // 4 numbers only message to the user, if number is shorter or longer than 4
                System.out.println("------------------------");
                System.out.println("Error, must be a 4 number pin only! Please retry:");
                System.out.println("------------------------");
                pinChoice = in.next();
            }

            for (i = 0; i < Names.length; i++) {
                //Check if PIN exists
                if (pinChoice.matches(pin[i])) {
                    userLogin = true;   //If the PIN exists (true) the user is brought to the main menu
                    System.out.println("------------------------");
                    System.out.println("Please select a option from the menu...");
                    sub = i;        //sub is the array number stored of that account
                }//end if PIN matches
            }


            if (userLogin == false) {   //If the PIN doesn't exist
                System.out.print("\nPin does not exist\n\n");   //Tell the user and bring them back to the main menu
            }//end if no user found

            break;

        }// end loop menuChoic == 1

        while (menuChoice == 2) {       //user chooses to Leave the program
            System.out.print("\nThank you, goodbye\n");
            System.exit(0);             //Exits the program
            break;
        }//end menuChoice==2

        //--------------------
        //end login script
        //--------------------
    }
}

我知道程序的其余部分运行良好。我就是拿不到这一小部分。

请记住,这是我在 Comp 的第一年。科学。而且我的讲师没有向我们展示任何面向对象,因此他没有让我们只学习基础知识。

感谢您的关注!:)

4

4 回答 4

1

两个小时后,我想通了,并在登录过程中添加了一个额外的步骤。

public static void main(String args[]) {

        Scanner in = new Scanner(System.in);

        boolean userLogin = false;          //Flag for user login authorization 
        boolean progOn=true;                //confirmation that program is still be running         

        int num=0;
        int Bal[] = {500,250,400,700};
        String Names[]={"Niall", "Chris", "John", "Connor"};
        String pin[]= {"1111","2222","3333","4444"};
        String menu2= "\n1. Deposit \n2. Withdrawal \n3. Balance \n4: Change pin \n5: Exit";
        String pinChoice = "", pinChange="", strNameChoice="", startMenu="";

        int sub=0, sub2=0;                              //To keep a subscript           
        int menuChoice=0 ;                              //The entered choice for the menu and name as an int
        int nameChoice=0;

        //--------------------
        //login script
        //--------------------

        while(progOn==true){
            System.out.println("------------------------");
            System.out.println("Welcome! Please select an option...\n>1. Login\n>2. Quit");
            System.out.println("------------------------");

            startMenu=in.next();

            while(!startMenu.matches("[1-2]")){                         //If the user enters a non digit, give message and return

                System.out.println("\nError. Please enter a valid Menu option.\n>1. Login\n>2. Quit\nPlease select an option\n"); //Ask the user to choose again    
                startMenu=in.next();
            }//end while loop

            menuChoice=Integer.parseInt(startMenu);                     //Parse the string to an int

            while(userLogin==false){                                    // loop to verify that the login is correct, and a rest point.

                while(menuChoice==1){                                       //While loop to carry out the users choice

                    System.out.println("------------------------");//User name selection
                    System.out.println("Please select the number of your user name. \n1> " +Names[0]+"\n2> " +Names[1]+"\n3> " +Names[2]+"\n4> " +Names[3]);
                    System.out.println("------------------------");
                    strNameChoice=in.next();

                    while(!strNameChoice.matches("[1-4]")){                  // 4 numbers only message to the user, if number is shorter or longer than 4
                        System.out.println("------------------------");
                        System.out.println("Error, must be a one of the 4 numbers only! Please retry:");
                        System.out.println("------------------------");
                        strNameChoice=in.next();
                    }


                    nameChoice=Integer.parseInt(strNameChoice);
                    sub2=nameChoice-1;



                    System.out.println("------------------------");  //PIN ENTRY
                    System.out.print("Please enter your Pin\n");
                    pinChoice = in.next();

                    while(!pinChoice.matches("\\d+")){
                        System.out.print("\nError, digits only\n");
                        System.out.print("\nEnter your PIN\n>");
                        pinChoice = in.next();
                    }//end while PIN is non digits

                    for(int i=0;i<Names[sub2].length();i++){                            //Check if PIN exists inside the loop

                        if(pinChoice.matches(pin[sub2])){
                            userLogin = true;                           //If the PIN exists (true) the user is brought to the main menu
                            sub = sub2-1;                                   //sub is the array number stored of that account
                        }//end if PIN matches
                    }       

                    if(userLogin == false){                                 //If the PIN doesn't exist
                        System.out.print("\n------------------------");
                        System.out.print("\nPin entered is incorrect \nPlease try again \n");

                    }//end if no user found
                    break;//Tell the user and bring them back to the main menu

                }// end loop menuChoice==1

                while(menuChoice==2){                   //user chooses to Leave the program

                    System.out.print("\nThank you, goodbye\n");
                    System.exit(0);                             //Exits the program
                    break;
                }//end menuChoice==2
        }   



                //--------------------
                //end login script
                //--------------------

我的简单错误是我没有在循环中标记登录是否为真,从而使break;, 用于不正确的 pin 无用。

while(userLogin==false){                                    // loop to verify that the login is correct, and a rest point.

                while(menuChoice==1){   
于 2014-03-10T23:40:36.510 回答
1

在循环中,您应该使用局部变量。你有一个全局变量

int i;    

尝试在循环中将i更改为int ii

 for (int ii = 0; ii < Names.length; ii++) {
   //Check if PIN exists
   if (pinChoice.matches(pin[ii])) {
       userLogin = true;   //If the PIN exists (true) the user is brought to the main menu
       System.out.println("------------------------");
       System.out.println("Please select a option from the menu...");
       sub = ii;        //sub is the array number stored of that account
   }//end if PIN matches
 }
于 2014-03-10T14:19:25.180 回答
0

您的 while 循环条件是while (menuChoice ==1),但在循环内的值menuChoice从不改变。因此,循环永远无法逃脱。您需要做的是更改 的值或在不存在 PIN 的部分中menuChoice添加行,以便在达到该逻辑时退出循环。break;

我认为最干净的解决方案是将您break;移到 if 条件内userLogin == false

祝你好运!

于 2014-03-10T14:15:11.690 回答
0

代替

 if (pinChoice.matches(pin[i])) 

 if (pinChoice.equals(pin[i])) 

要检查 pinChoice 是否等于 arrayElement,请使用equals方法。

还有下面循环的迭代:

for (i = 0; i < Names.length; i++) 

必须迭代到pin数组的大小而不是Names长度

于 2014-03-10T14:15:49.687 回答