Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
限制Java中的登录尝试_Java_Loops_Login - Fatal编程技术网

限制Java中的登录尝试

限制Java中的登录尝试,java,loops,login,Java,Loops,Login,我正在尝试将最大登录尝试次数限制为3次。然而,我下面的代码在用户有机会再次按下登录按钮之前使用了所有尝试。我该如何解决这个问题 private void executeLogin() { String userNameStr = userNameTF.getText(); String passWordStr = passWordTF.getText(); int totalAttempts = 3; while (totalAttempts != 0) {

我正在尝试将最大登录尝试次数限制为3次。然而,我下面的代码在用户有机会再次按下登录按钮之前使用了所有尝试。我该如何解决这个问题

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");
    }
}

您的问题是while循环不会等待用户再次按下按钮。假设每次按下按钮时都调用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");
    }
}

您的问题是while循环不会等待用户再次按下按钮。假设每次按下按钮时都调用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");
    }
}

您的代码只执行相同的登录次数。你不能放弃控制权。在事件驱动编程中,您不必等待用户做某件事,而是对其进行设置并根据用户做的某件事进行响应。换句话说,您永远不会编写主动等待用户再次输入密码的代码


最重要的是,没有“将登录次数限制为3”这样的事情。您可以将连续不正确登录的次数限制为3次,您可以将连续不正确用户登录的次数限制为3次,您可以将每个时段的不正确登录次数限制为3次-所有这些都需要一些数据结构来保存有关失败登录的信息

您的代码只执行相同的登录次数。你不能放弃控制权。在事件驱动编程中,您不必等待用户做某件事,而是对其进行设置并根据用户做的某件事进行响应。换句话说,您永远不会编写主动等待用户再次输入密码的代码


最重要的是,没有“将登录次数限制为3”这样的事情。您可以将连续不正确登录的次数限制为3次,您可以将连续不正确用户登录的次数限制为3次,您可以将每个时段的不正确登录次数限制为3次-所有这些都需要一些数据结构来保存有关失败登录的信息

代码中的问题是,您没有要求用户再次输入用户名和密码。我的解决办法是:

设计函数,使其在循环中请求凭据

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");
    }
}

代码中的问题是,您没有请求用户再次输入其用户名和密码。我的解决办法是:

设计函数,使其在循环中请求凭据

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");
    }
}

因此,您的代码存在3个主要问题:

  • 您使用while循环——虽然您根本不应该循环,但每次按下登录按钮时都应该调用该函数

  • 尝试次数不能是局部变量-您应该保留其值以备将来使用(因此为全局变量)

  • 您以错误的方式比较字符串(不是
    =
    ,而是
    等于


  • 因此,您的代码存在3个主要问题:

  • 您使用while循环——虽然您根本不应该循环,但每次按下登录按钮时都应该调用该函数

  • 尝试次数不能是局部变量-您应该保留其值以备将来使用(因此为全局变量)

  • 您以错误的方式比较字符串(不是
    =
    ,而是
    等于

  • 无论何时调用executeLogin(),totalAttempts的上一个值都将被擦除,并再次初始化为3。要控制这一点,您可以将totalAttempts设置为全局

    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");
        }
    }
    
    或者,如果您在类中声明它,则将其设置为静态

    无论何时调用executeLogin(),totalAttempts的上一个值都将被擦除,并再次初始化为3。要控制这一点,您可以将totalAttempts设置为全局

    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");
        }
    }
    

    或者,如果您在类中声明它,则将其设置为静态

    我猜
    userNameTF
    passWordTF
    都是文本字段-所以你有一些UI。。。实际上,这里不需要循环。仅当用户按下登录按钮时才应调用此函数。totalAttempts应该是全局变量…我猜
    userNameTF
    passWordTF
    是文本字段-所以你有一些UI。。。实际上,这里不需要循环。仅当用户按下登录按钮时才应调用此函数。totalAttempts应该是全局变量…这将有相同的问题-因为用户永远不会有机会更改其输入…userNameTF.getText()是一个等待用户输入的函数吗?如果没有从命令行读取用户名和密码,如
    Java BufferedReader=new BufferedReader(new InputStreamReader(System.in));System.out.print(“输入您的用户名:”);字符串username=reader.readLine();System.out.print(“\n输入密码:”);字符串密码=reader.readLine()这将有同样的问题-因为用户永远不会有机会更改其输入…userNameTF.getText()是等待用户输入的函数吗?如果没有从命令行读取用户名和密码,如
    Java BufferedReader=new BufferedReader(new InputStreamReader(System.in));System.out.print(“输入您的用户名:”);字符串username=reader.readLine();System.out.print(“\n输入密码:”);字符串密码=reader.readLine()谢谢Neha提供的代码。虽然其他人也回答了这个问题,并提供了解决方案,但你的回答经过了深思熟虑,提出了一些我应该做什么和不应该做什么的建议。因此,我将选择你的答案作为最佳答案。谢谢大家!谢谢你的代码。虽然其他人也回答了这个问题并提供了解决方案,但你的回答是公平的