[英]how to Terminate program if user inputs invalid username and password several times
The problem in this code is that it doesn't terminate the program even though I clicked the login button with a invalid username and password 3 times. 此代码中的问题是,即使我用无效的用户名和密码单击登录按钮3次,它也不会终止程序。 So what I want to happen is that when the user inputs a invalid username and password 3 times, it will terminate the program.
所以我想发生的是,当用户输入无效的用户名和密码3次时,它将终止程序。
MessageBox.Show("Invalid username and/or password.");
pass_txt.Text = null;
int login_count = 0;
login_count = login_count + 1;
if (login_count == 3)
{
Application.Exit();
}
int login_count = 0;
login_count = login_count + 1;
Login_count will never be greater than 1, since you initialize it to 0 right before you increment it. Login_count永远不会大于1,因为您在将其递增之前将其初始化为0。 You need to make it global, then increment it without setting it back to 0.
您需要将其设置为全局,然后递增而不将其设置回0。
You'll need something closer to 您将需要一些更接近的东西
if(failedLogin)
{
// ...
this.login_count++;
}
// ...
if (this.login_count == 3)
{
Application.Exit();
}
Something like this might work. 这样的事情可能会起作用。
private void Login()
{
int login_count = 0;
bool goodLogin = false;
do
{
if (DoLogin())
goodLogin = true;
else
login_count++;
} while (login_count < 4 && !goodLogin);
if (goodLogin)
{
//do the login stuff
}
else
{
Application.Exit();
}
}
private bool DoLogin()
{
if (true) //do the login logic here
return true;
else
{
MessageBox.Show("Invalid username and/or password.");
pass_txt.Text = null;
return false;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.