import java.util.Scanner;
public class ATMMachine {
// Declared global variables.
public static int IDattempts = 3, pinattempts = 3;
public static double balance;
public static String ID = "John Smith", pin = "1234";
public static int freeze = 0;
public static void main(String[] args) {
IDProcedure();
}
static Scanner input = new Scanner(System.in);
// ID Procedure
public static void IDProcedure() {
while (freeze == 0) {
System.out.print("Please enter your ID: ");
ID = input.nextLine();
// Ignores case, so if typed in upper/lower , program still works.
if (ID.equalsIgnoreCase("john smith")) {
PINProcedure();
} else {
IDattempts -= 1;
System.out.println("You have " + IDattempts + " more tries to enter your correct ID");
}
if (IDattempts == 0) {
System.out.print("You are out of tries, you will be kicked out of the program.");
System.exit(0);
}
}
}
// PIMProcedure
public static void PINProcedure() {
while (freeze == 0) {
System.out.print("Please enter your PIN: ");
pin = input.nextLine();
if (pin.equals("1234")) {
System.out.print("You're in!");
// Here is where you make them see their balance, withdrawal
} else {
pinattempts -= 1;
System.out.println("You have " + pinattempts + " more tries to enter your correct PIN");
System.exit(0);
}
}
}
}
您好,
所以我正在创建一个类似atm机器的程序,但我现在需要解释的就是我所拥有的。
我有,所以当有人打开程序时,会要求他们输入ID(用户名)和密码(4位数密码)。
一切都很有效,如果您在被要求 ID 时尝试了3次尝试,程序就会终止。
如果您在请求 PIN 时尝试用完,则会终止。有没有办法解决这个问题,我确信这是,我需要社区的帮助!提前致谢。如果您发现更多错误或初学者错误,请通知我,因为我是新人。
答案 0 :(得分:0)
仔细查看您的PINProcedure()
方法。在IDProcedure()
中,你有这个:
if (IDattempts == 0) {
System.out.print("You are out of tries, you will be kicked out of the program.");
System.exit(0);
}
PINProcedure()
缺少此条件语句,因此如果输入了错误的PIN,它将达到System.exit(0)
,无论剩余多少次尝试。你只需像在IDProcedure()
中那样在这里包含条件语句,一切都应该正常工作。