【问题标题】:How to make a Password and User request with a maximum of 4 attempts in Java?如何在 Java 中进行最多 4 次尝试的密码和用户请求?
【发布时间】:2017-11-21 15:23:41
【问题描述】:

我目前遇到一个问题。我正在为收银员编写代码,但我无法解决密码和用户请求的问题,在收银员关闭之前最多应该有 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();
        }
    }

【问题讨论】:

  • 似乎您从未将输入的密码值与预期值进行比较。您还允许使用 0 长度密码:CLIENT.length() != 16,这肯定不是您想要的。

标签: java passwords compare


【解决方案1】:

您的密码长度条件和 while 循环是相反的。

...
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();
    }
}
...

【讨论】:

  • 我很确定 OP 还想要一个最小长度为 4 的密码,而不是 "anything but not exactly length() == 4"。
  • 没错,有几件事可以解决。这仅涵盖 4 次无效尝试。
【解决方案2】:

感谢我的朋友 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 ++;
        } 

【讨论】:

    猜你喜欢
    • 2023-04-01
    • 1970-01-01
    • 2023-03-27
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    • 2018-05-01
    • 1970-01-01
    • 2019-08-14
    相关资源
    最近更新 更多