简单Java银行Atm登录循环错误

简单Java银行Atm登录循环错误,java,bank,Java,Bank,第一次发帖,这里的人似乎很有帮助。 我目前正在编写我的第二份简单银行atm/账户作业。我的用户pin登录代码没有执行以下操作 1。在阵列中找不到管脚时返回到循环的开始处 2。如果pin不正确,使程序停止 以下是登录脚本的片段。问题在于while(menuChoice==1){}循环 public static void main(String args[]) { Scanner in = new Scanner(System.in); boolean userLogin = f

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

1。在阵列中找不到管脚时返回到循环的开始处

2。如果pin不正确,使程序停止

以下是登录脚本的片段。问题在于
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
        //--------------------
    }
}
publicstaticvoidmain(字符串参数[]){
扫描仪输入=新扫描仪(系统输入);
布尔userLogin=false;//用户登录授权标志
boolean progOn=true;//确认程序仍在运行
int num=0,i=0;
int Bal[]={500250400700};
字符串名[]={“尼尔”、“肖恩”、“约翰”、“康纳”};
字符串引脚[]={“1234”、“2345”、“3456”、“4567”};
字符串menu2=“\n1.存款\n2.取款\n3.余额\n4:更改pin码\n5:退出”;
字符串pinChoice=“”,pinChange=“”;
int sub=0;//保留下标
String startMenu=“”;//作为字符串输入的开始菜单选项
int menuChoice=0;//输入的菜单选项为int
//--------------------
//登录脚本
//--------------------
while(progOn==true){
System.out.println(“---------------------------”);
System.out.println(“欢迎!请选择一个选项…\n”);
System.out.println(“>1.Login”);
System.out.println(“>2.Quit”);
System.out.println(“---------------------------”);
startMenu=in.next();
而(!startMenu.matches(“[1-2]”)){//如果用户输入非数字,则给出消息并返回
System.out.println(“\n错误。请输入有效的菜单选项。\n”);
System.out.println(“>1.Login”);
System.out.println(“>2.Quit”);
System.out.print(“请选择一个选项”;//请用户再次选择
startMenu=in.next();
}//边结束边循环
menuChoice=Integer.parseInt(startMenu);//将字符串解析为int
while(menuChoice==1){//while循环执行用户选择
System.out.println(“---------------------------”);
System.out.println(“请输入您的pin码:”);
System.out.println(“---------------------------”);
pinChoice=in.next();
而(!pinChoice.matches(“\\d{4}”){//4只向用户发送数字消息,如果数字小于或大于4
System.out.println(“---------------------------”);
System.out.println(“错误,只能是4号pin!请重试:”);
System.out.println(“---------------------------”);
pinChoice=in.next();
}
对于(i=0;i
我知道程序的其余部分工作得很好。我就是拿不到这个小零件

记住这是我在公司的第一年。Sci。我的讲师没有向我们展示任何面向对象的方法,因此他不会让我们看到任何东西,只是基本的东西


谢谢你的关注!:)

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

我认为最干净的解决办法是移动你的
断点在if条件下的to
userLogin==false

祝你好运

更换

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

要检查夹选是否等于阵列元素,请使用等于方法

此外,for循环的迭代如下所示:

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

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

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

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
 }
for(int ii=0;ii
整整两个小时后,我终于明白了这一点,并在登录过程中添加了一个额外的步骤

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
                //--------------------

谢谢你的意见。虽然我知道它应该在for循环中,但它确实没有
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
                //--------------------
while(userLogin==false){                                    // loop to verify that the login is correct, and a rest point.

                while(menuChoice==1){