gpt4 book ai didi

java - 简单的 Java Bank Atm 登录循环错误

转载 作者:行者123 更新时间:2023-11-30 09:10:56 26 4
gpt4 key购买 nike

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

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

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

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

感谢观看! :)

最佳答案

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

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
}

关于java - 简单的 Java Bank Atm 登录循环错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22302143/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com