我在这个节目中工作了3个小时,但我不知道我在哪里做错了。如果你能帮助我真的很感激。问题是当我输入密码时。即使我输入了正确的密码它也是错误的密码,它不允许我再次重试。该程序假设允许用户尝试3次如果用户输错密码第三次该程序必须关闭。
public partial class UserAndPin : Window
{
public UserAndPin()
{
InitializeComponent();
}
private void btnOK_Click(object sender, RoutedEventArgs e)
{
try
{
StreamReader sr = new StreamReader("Customer.txt");
short attempts = 0;
string line;
while ((line = sr.ReadLine()) != null)
{
string[] lineArray = line.Split(';');
if (lineArray[0] == txtName.Text & lineArray[1] == pbPassword.Password)
{
MainWindow mainWindow = new MainWindow();
this.Hide();
mainWindow.ShowDialog();
//return;
}
else
{
attempts++;
if (attempts < 3)
{
MessageBox.Show("The NAME or PIN is incorect, you have " + (3 - attempts) + " attemps more");
}
if (attempts == 3)
{
MessageBox.Show("Please try again later");
this.Close();
}
}
}
sr.Close();
}
catch (Exception error)
{
MessageBox.Show(error.Message);
}
}
}
}
答案 0 :(得分:1)
由于您每次点击按钮时都会在short attempts = 0;
内声明btnOK_Click
,因此attempts
会0
为public partial class UserAndPin : Window
{
short attempts;
public UserAndPin()
{
InitializeComponent();
attempts = 0;
}
,而您希望每次增加1 {1}}用户单击按钮,因此您需要将其全局声明为
attempts++;
attempt
应低于while循环,因为如果文件中有10个用户信息,则每次条件不匹配时它将添加或增加Logical AND operator
。
现在,如果用户名和密码与您正在阅读的文件不匹配。如果用户信息位于第10个位置或行,它显然不匹配,它将为您提供9次消息框。另一个问题是&&
它是&
而不是public partial class UserAndPin : Window
{
short attempts;
public UserAndPin()
{
InitializeComponent();
attempts = 0;
}
private void btnOK_Click(object sender, RoutedEventArgs e)
{
try
{
StreamReader sr = new StreamReader("Customer.txt");
string line;
while ((line = sr.ReadLine()) != null)
{
string[] lineArray = line.Split(';');
if (lineArray[0] == txtName.Text && lineArray[1] == pbPassword.Password)
{
MainWindow mainWindow = new MainWindow();
this.Hide();
mainWindow.ShowDialog();
//return;
}
}
sr.Close();
if (attempts < 3)
{
MessageBox.Show("The NAME or PIN is incorect, you have " + (3 - attempts) + " attemps more");
}
else
{
MessageBox.Show("Please try again later");
this.Close();
}
attempts++; //Since user has attempted it.
}
catch (Exception error)
{
MessageBox.Show(error.Message);
}
}
}
而且它会匹配所以正确的方法应该是
SELECT * FROM (
SELECT DISTINCT cases.caseid, cases.img, casetypes.casetypeid, cases.cost
FROM cases, casetypes
WHERE cases.caseid = casetypes.caseid AND casetypes.mastercaseid = $mastercaseid
LIMIT $lastrow, $perPage
) x
ORDER BY RAND()
答案 1 :(得分:1)
你应该开始尝试&#39;每次认证成功。或3小时内超过3次。
如果您在2次成功登录,则尝试&#39;应该是0。
如果项目拒绝您登录的时间超过3小时,则“尝试”会尝试&#39;也应该设置为0。
答案 2 :(得分:1)
首先,您需要阅读文件以获取所有用户名和密码。理想情况下,您只能在构造函数中执行此操作一次。
然后,您需要将计数器增加一个。此计数器需要在click事件处理程序之外声明,如其他答案中所述。
最后,您可以检查输入的用户/密码是否与文件中的一个匹配。如果是,您可以打开表单。如果没有,则根据用户是否已达到第三次尝试,显示其中一个消息框。
public partial class UserAndPin : Window
{
short attempts;
public UserAndPin()
{
InitializeComponent();
attempts = 0;
}
private void btnOK_Click(object sender, RoutedEventArgs e)
{
try
{
var users = File.ReadAllLines("Customer.txt")
.Select(line => new { login = line[0], password = line[1] })
.ToList();
attempts++;
if (users.Any(user => user.login == txtName.Text && user.password == pbPassword.Password))
{
MainWindow mainWindow = new MainWindow();
this.Hide();
mainWindow.ShowDialog();
return;
}
else
{
if (attempts < 3)
{
MessageBox.Show("The NAME or PIN is incorect, you have " + (3 - attempts) + " attemps more");
}
if (attempts >= 3)
{
MessageBox.Show("Please try again later");
this.Close();
}
}
}
catch (Exception error)
{
MessageBox.Show(error.Message);
}
}
}
答案 3 :(得分:0)
Dictionary<string, string> loginInfo;
short attempts = 0;
public UserAndPin()
{
InitializeComponent();
// Load the file to the dictionary
loginInfo = File.ReadAllLines("Customer.txt")
.Select(i => i.Split(';')) // Lines format: Username;Password
.ToDictionary(i => i[0].ToLower(), i => i[1]); // Username is the key of the dictionary
}
private void btnOK_Click(object sender, RoutedEventArgs e)
{
var userId = txtName.Text.ToLower(); // Username ignore case
var password = pbPassword.Password;
if (loginInfo.ContainsKey(userId) && loginInfo[userId] == password)
{
// login success, show main window
MainWindow mainWindow = new MainWindow();
this.Hide();
mainWindow.ShowDialog();
return;
}
// login fail, increment the count only
attempt++;
if (attempts < 3)
{
MessageBox.Show("The NAME or PIN is incorect, you have " + (3 - attempts) + " attemps more");
}
if (attempts == 3)
{
MessageBox.Show("Please try again later");
this.Close();
}
}