第一次发帖,这里的人似乎很有帮助。我目前正在编写我的第二个简单银行 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 的第一年。科学。而且我的讲师没有向我们展示任何面向对象,因此他没有让我们只学习基础知识。
感谢您的关注!:)