- private int EerroTime = 0;
- string useName = textBox1.Text;
- string passWard = textBox2.Text;
- for (int i = 0; i < 3; i++)
- {
- if ((useName == "admin") && (passWard == "8888"))
- {
- MessageBox.Show("登陆成功");
- break;
- }
- else
- {
- EerroTime++;
- if (EerroTime < 3)
- {
- MessageBox.Show("登录失败");
- break;
- }
- else
- {
- MessageBox.Show("登录次数过多");
- break;
- }
- }
- this.Close();
- }
复制代码 关键是在方法外部定义EerroTime 变量的值,使其在随着for循环自增的情况下,而不每次都要初始化 |