简体   繁体   English

在 Java 中限制登录尝试

[英]Limit login attempt in Java

I am trying to limit the maximum login attempts to 3. However, my code below uses all the attempts before the user can get a chance to press the login button again.我试图将最大登录尝试次数限制为 3。但是,我下面的代码在用户有机会再次按下登录按钮之前使用了所有尝试。 How would I fix this?我将如何解决这个问题?

private void executeLogin() {

    String userNameStr = userNameTF.getText();
    String passWordStr = passWordTF.getText();

    int totalAttempts = 3;

    while (totalAttempts != 0) {

        if (userNameStr == "temp" && passWordStr == "pass") {


            System.out.println("Login Correct!");
            return;

        } else {


            System.out.println("Incorrect Login");

            totalAttempts--;
            System.out.println(totalAttempts);

        }

    }

    if (totalAttempts == 0) {

        System.out.println("Maximum number of attempts exceeded");
    }
}

Whenever, the executeLogin() will be invoked, the previous value of totalAttempts will be erased and it will be again initialized to 3. To control this, you can make the totalAttempts as global 每当调用executeLogin()时,totalAttempts的先前值将被清除,并将其再次初始化为3。要控制此情况,可以将totalAttempts设置为global

int totalAttempts= 3; 
private void executeLogin() {

    String userNameStr = userNameTF.getText();
    String passWordStr = passWordTF.getText();

    if (totalAttempts != 0) {
        if (userNameStr == "temp" && passWordStr == "pass") {
            System.out.println("Correct");
        else {
            System.out.println("Incorrect");
            totalAttempts--;

    } else {
        System.out.println("Maximum number of attempts exceeded");
    }
}

Or if you are declaring it inside the class make it static. 或者,如果您要在类中声明它,请使其变为静态。

So there are 3 main problems with your code: 因此,您的代码存在3个主要问题:

  1. You use while loop - though you shouldn't loop at all, function should be called every time login button is pressed. 您使用while循环-尽管您根本不应该循环,但每次按下登录按钮时都应调用函数。

  2. Number of attempts cannot be local variable - you should keep its value for future use (so global variable then) 尝试次数不能是局部变量-您应保留其值以备将来使用(然后使用全局变量)

  3. You are comparing string the wrong way (not == but equals ) 您正在以错误的方式比较字符串(不是==equals

     public class MyForm extends ... { int totalAttempts = 3; private void login() { String userNameStr = userNameTF.getText(); String passWordStr = passWordTF.getText(); if (totalAttempts != 0) if ("temp".equals(userNameStr) && "pass".equals(passWordStr)) System.out.println("Correct"); else { System.out.println("Incorrect"); totalAttempts--; } else System.out.println("Maximum number of attempts exceeded"); } } 

Your problem is that the while loop doesn't wait for the user to press the button again. 您的问题是while循环不会等待用户再次按下按钮。 Assuming that executeLogin() is called every time the button is pressed, you need to keep a global attempts variable and decrement it every time the method is called rather than multiple times within the same call. 假设每次按下按钮都会调用executeLogin(),则需要保留一个全局尝试变量,并在每次方法调用时将其减小,而不是在同一调用中多次。

int totalAttempts = 3;

private void executeLogin() {

    String userNameStr = userNameTF.getText();
    String passWordStr = passWordTF.getText();

    if (totalAttempts != 0) {
        if (userNameStr == "temp" && passWordStr == "pass") {
            System.out.println("Correct");
        else {
            System.out.println("Incorrect");
            totalAttempts--;

    } else {
        System.out.println("Maximum number of attempts exceeded");
    }
}

Your code just performs the same login totalAttempts times. 您的代码仅执行相同的登录totalAttempts时间。 You don't cede control. 您不会放弃控制权。 In event-driven programming, you do not wait for the user to do something, you set it up and do things in response to a user doing something. 在事件驱动的编程中,您不必等待用户做某事,而是将其设置并做事以响应用户做某事。 In other words, you never write a code that actively waits for a user to input the password again. 换句话说,您永远不会编写主动等待用户再次输入密码的代码。

The main thing is, there is no such thing as "limiting the number of logins to 3". 最主要的是,没有诸如“将登录数限制为3”之类的东西。 You can limit the number of consecutive incorrect logins to 3, you can limit the number of consecutive incorrect user logins to 3, you can limit the number of incorrect logins per period to 3 - all of those require some data structure to hold the information about the failed logins. 您可以将连续不正确的登录次数限制为3,可以将连续不正确的用户登录次数限制为3,可以将每个周期不正确登录次数限制为3-所有这些都需要某种数据结构来保存有关登录失败。

The problem in your code is that you don't request the user to enter his username and password again. 您的代码中的问题是您不要求用户再次输入其用户名和密码。 My fix would be: 我的解决办法是:

Design the function so that it requests the credentials in the loop 设计函数,使其在循环中请求凭据

private void executeLogin() {

    String userNameStr;
    String passWordStr;

    int totalAttempts = 3;

    while (totalAttempts != 0) {

        userNameStr = userNameTF.getText();
        passWordStr = passWordTF.getText();

        if (userNameStr == "temp" && passWordStr == "pass") {

            System.out.println("Login Correct!");
            return;

        } else {


            System.out.println("Incorrect Login");

            totalAttempts--;
            System.out.println(totalAttempts);

        }

    }

    if (totalAttempts == 0) {

        System.out.println("Maximum number of attempts exceeded");
    }
}
package javaconcepts;

import java.util.Scanner;

public class JavaLoginPrg {

    private void login() {
        String username, password;
        int totalAttempts = 3;
        while (totalAttempts != 0) {

            if (username == "temp" && password == "pass") {
                System.out.println("Login succeeded!");
                return;
            } else {
                System.out.println("Login failed!");
                totalAttempts--;
                System.out.println(totalAttempts);
            }
        }
    }

    public static void main(String[] args) {
        String temp, pass;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter username");
        username = sc.toString();
        System.out.println("Enter password");
        password = sc.toString();
    }
}

Tried this with user prompted inputs and it actually works import java.util.Scanner;使用用户提示输入进行了尝试,它实际上可以工作 import java.util.Scanner;

public class AuthenticateUser {公共 class AuthenticateUser {

public static void main(String[] args) {

    int attempts=0;
    String password="password";
    Scanner obj = new Scanner(System.in);

    while (true)
    {

        if(attempts!=5)
        {
            System.out.println("Enter your password ");
            String inputPassword = obj.nextLine();

            if (inputPassword.equals(password)) {
                System.out.println("You have successful logged in!");
                break;
            } else {
                System.out.println("Something went wrong, verify your password and try again!");
                attempts++;
            }
        } else {
            System.out.println("You incorrectly logged in 5 times, you will have to wait for 30 minutes");
            break;
        }

    }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM