在 Java 中限制登录尝试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39001793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Limit login attempt in Java
提问by Milan
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. How would I fix this?
我试图将最大登录尝试次数限制为 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");
}
}
回答by Neha Dadhich
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 设为全局
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.
或者,如果您在类中声明它,请将其设为静态。
回答by Plirkee
So there are 3 main problems with your code:
因此,您的代码存在 3 个主要问题:
You use while loop - though you shouldn't loop at all, function should be called every time login button is pressed.
Number of attempts cannot be local variable - you should keep its value for future use (so global variable then)
You are comparing string the wrong way (not
==
butequals
)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"); } }
您使用 while 循环 - 尽管您根本不应该循环,但每次按下登录按钮时都应调用该函数。
尝试次数不能是局部变量 - 您应该保留其值以备将来使用(因此是全局变量)
您正在以错误的方式比较字符串(不是
==
但是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"); } }
回答by Superhq 2000
Your problem is that the while loop doesn't wait for the user to press the button again. 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.
您的问题是 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");
}
}
回答by Piotr Wilkin
Your code just performs the same login totalAttempts
times. 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.
您的代码只是执行相同的登录totalAttempts
时间。你不放弃控制权。在事件驱动编程中,您不是等待用户做某事,而是设置它并响应用户做某事而做事。换句话说,您永远不会编写主动等待用户再次输入密码的代码。
The main thing is, there is no such thing as "limiting the number of logins to 3". You can limit the number of consecutive incorrect loginsto 3, you can limit the number of consecutive incorrect user loginsto 3, you can limit the number of incorrect logins per periodto 3 - all of those require some data structure to hold the information about the failed logins.
主要的是,没有“将登录次数限制为 3”这样的东西。您可以将连续错误登录的次数限制为 3,您可以将用户连续错误登录的次数限制为 3,您可以将每个周期的错误登录次数限制为 3 - 所有这些都需要一些数据结构来保存有关信息失败的登录。
回答by Kostas Pelelis
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");
}
}