我目前陷入了问题。我正在为收银员做代码,但我无法解决密码和用户请求的问题,在收银员关闭之前,它应该最多进行4次尝试。如果有人可以帮助我解决这个问题,我真的很感激。
public static void login(){
int TIMESC = 0;
int TIMESP = 0;
String PROMT;
Scanner keyboard = new Scanner(System.in); //Keyboard input initializer
PROMT = ">"; //promt sring definition
System.out.println("Welcome to LJD Bank");
System.out.println("Insert Bank account");
System.out.println();
System.out.printf(PROMT);
CLIENT = keyboard.nextLine(); //to be defined by user
if(CLIENT.length() != 16){
if(TIMESC == 0){
while(TIMESC < 4){
System.out.println("Not a valid Account");
System.out.println("Please insert a valid Account");
System.out.println(PROMT);
CLIENT = keyboard.nextLine();
TIMESC ++;
}
Cashier.close();
}
}
else{
System.out.println("Insert NIP");
System.out.printf(PROMT);
PASSWORD = keyboard.nextLine();
if(PASSWORD.length() != 4){
while(TIMESP < 4){
System.out.println("Not a valid NIP");
System.out.println("Please insert a valid NIP");
PASSWORD = keyboard.nextLine();
TIMESP ++;
}
Cashier.close();
}
}
您的密码长度条件和时循环倒置。
...
else{
System.out.println("Insert NIP");
System.out.printf(PROMT);
while(TIMESP < 4){
PASSWORD = keyboard.nextLine();
if(PASSWORD.length() != 4) {
System.out.println("Not a valid NIP");
System.out.println("Please insert a valid NIP");
TIMESP ++;
} else {
break;
}
}
if (TIMESP == 3) {
Cashier.close();
}
}
...
信用我的朋友Edvilme,他帮助了我解决这个问题。这是代码:
if(CLIENT.length() == 16){ //lenght
RETRY_CLIENT = 5;//this variable is set to 5 breaking the loop when the client is valid
while(RETRY_NIP < 4){//while the RETRYNIP is less than four then the nip is requested
System.out.println("Insert NIP");
System.out.printf(PROMT);
PASSWORD = keyboard.nextLine();//NIP is requested
System.out.println();
if(PASSWORD.length() == 4){ //if nip length is equal to 4
System.out.println("Welcome to your account user " +CLIENT); //gives welcome
RETRY_NIP = 5; //entonces se pone como 5 rompiendo el loop
}
else if(PASSWORD.length() != 4 && RETRY_NIP == 4){ //if the password length is diferent from 4 and retryNIP is equal to 4 attempts
Cashier.close(); //Cashier closes
}
else if(PASSWORD.length() != 4){// if the NIP Lenght is diferent from 4 then the NIP is requested again, RETRYNIP increases in 1
System.out.println(TRY);
RETRY_NIP ++; //RetryNIP is increased in one (+1)
}
}
}
else if(CLIENT.length() != 16 && RETRY_CLIENT == 4){ //IF the lenght of the user is diferent from 16 and the Retry Client equals then the cashier closes
Cashier.close();
}
else if(CLIENT.length() != 16){//if the clientlenght is diferent from 16 then the retry client increases in one (+1)
System.out.println(TRY);
RETRY_CLIENT ++;
}