所以我制作了一台ATM,我要编程的第一件事就是登录界面。为了定义用户,我创建了一个User类,它由id,用户名,密码,储蓄账户和支票账户组成。在我的Windows窗体中,我创建了两个按钮,一个执行登录,另一个关闭程序。这是我的Windows窗体的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ATM_Project
{
public partial class Form1 : Form
{
List<User> users = new List<User>();
int attempts = 3;
public Form1()
{
new User() { id = 0, userName = "user1", password = "password123", savingsAcct = 2000, checkAcct = 2500 };
new User() { id = 1, userName = "user2", password = "password234", savingsAcct = 3000, checkAcct = 4500 };
new User() { id = 2, userName = "user3", password = "pass345", savingsAcct = 3000, checkAcct = 5000 };
InitializeComponent();
}
private void exitbtn_Click(object sender, EventArgs e)
{
this.Close();// this button is used to close the application
}
private void loginbtn_Click(object sender, EventArgs e)
{
verification();
}
public void verification()
{
for (int i = 0; i < users.Count; i++)
{
while (attempts != 0)
{
if (textBox1.Text == users[i].userName && textBox2.Text == users[i].password) //checks that the username and the password match.
{
MessageBox.Show("Your password is correct!");
break;
}
else
{
MessageBox.Show("Error. Your username or password are incorrect!");
attempts = attempts - 1;
}
}
}
}
}
}
我将对象放在列表中,并使用for循环遍历列表,并将第一个文本框中的用户输入与第i个位置的用户名进行比较,并将第二个文本框中用户输入的内容进行比较第i个位置的密码。如果它们匹配,它应该弹出一条消息告诉我它是正确的。如果它错了,它应该告诉我它是错的,经过三次尝试它应该停止工作。我创建了一个名为验证的公共空白,我在那里进行所有测试,我只是在登录按钮中调用它。然而它没有用。当我在文本框中键入内容并单击登录按钮时,它什么都不做。但是退出按钮确实有效。任何关于为什么会发生这种情况的见解?有什么我可以忘记的吗?
答案 0 :(得分:1)
您似乎没有向用户列表变量添加任何内容...而不是:
public Form1()
{
new User() { id = 0, userName = "user1", password = "password123", savingsAcct = 2000, checkAcct = 2500 };
new User() { id = 1, userName = "user2", password = "password234", savingsAcct = 3000, checkAcct = 4500 };
new User() { id = 2, userName = "user3", password = "pass345", savingsAcct = 3000, checkAcct = 5000 };
InitializeComponent();
}
试
public Form1()
{
users.Add (new User() { id = 0, userName = "user1", password = "password123", savingsAcct = 2000, checkAcct = 2500 });
users.Add (new User() { id = 1, userName = "user2", password = "password234", savingsAcct = 3000, checkAcct = 4500 });
users.Add (new User() { id = 2, userName = "user3", password = "pass345", savingsAcct = 3000, checkAcct = 5000 });
InitializeComponent();
}
然后,当您遍历用户列表时,users.Count将有一个值。
答案 1 :(得分:0)
看起来您正在定义新用户,但未将其添加到列表中。 所以你循环0个用户。
您可以将Form1()更改为
public Form1()
{
users.add(new User() { id = 0, userName = "user1", password = "password123", savingsAcct = 2000, checkAcct = 2500 });
users.add(new User() { id = 1, userName = "user2", password = "password234", savingsAcct = 3000, checkAcct = 4500 });
users.add(new User() { id = 2, userName = "user3", password = "pass345", savingsAcct = 3000, checkAcct = 5000 });
InitializeComponent();
}
另请注意,this.Close()仅关闭窗口,而不关闭应用程序。正如Winforms: Application.Exit vs Enviroment.Exit vs Form.Close
中所述我认为此版本的验证可能更有意义:
public void verification()
{
if (textBox1.Text == users[i].userName && textBox2.Text == users[i].password) //checks that the username and the password match.
{
MessageBox.Show("Your password is correct!");
}
else
{
MessageBox.Show("Error. Your username or password are incorrect!");
attempts -= 1;
}
if(attempts == 0)
{
Environment.Exit();
}
}