如何打破 do while 循环?
How to break a do while loop?
我是编码新手,一直在尝试使用文本文件进行登录(我知道我知道使用文本文件进行练习并不安全)
namespace text_file_login
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
private void btn_register_Click(object sender, EventArgs e)
{
StreamWriter user_reg =
new StreamWriter(@"E:\SSD\Controled Assesment\text file login\text file login\username.txt");
StreamWriter pass_reg =
new StreamWriter(@"E:\SSD\Controled Assesment\text file login\text file login\password.txt");
user_reg.WriteLine(txt_user.Text);
user_reg.Close();
pass_reg.WriteLine(txt_pass.Text);
pass_reg.Close();
MessageBox.Show("Register was sucessful");
}
private void btn_login_Click(object sender, EventArgs e)
{
string user = "";
string pass = "";
int user_line_number = 0;
int pass_line_number = 0;
StreamReader user_login =
new StreamReader(@"E:\SSD\Controled Assesment\text file login\text file login\username.txt");
StreamReader pass_login =
new StreamReader(@"E:\SSD\Controled Assesment\text file login\text file login\password.txt");
do
{
user = user_login.ReadLine();
user_line_number++;
if (user == txt_user.Text)
{
do
{
pass = pass_login.ReadLine();
pass_line_number++;
if ((pass == txt_pass.Text) && (user_line_number == pass_line_number))
{
MessageBox.Show("Login was succesful");
//this.Close();
}
else
{
if ((user_line_number > pass_line_number) || (user_line_number < pass_line_number))
{
MessageBox.Show("Wrong Password");
}
}
} while (pass_login.Peek() != -1);
}
} while (user_login.Peek() != -1);
}
}
}
登录成功,注册成功。但是,如果用户名或密码错误,我很确定这将永远循环。我如何结束循环,以便在用户名或密码错误时显示消息框?
我不太关心多个用户名,但是如果你可以解决这个问题?
抱歉,如果写得不好。我是初学者
如果你想 "break" 跳出循环,你真的可以这样做:
do {
if (something)
break;
}
while (condition)
我是编码新手,一直在尝试使用文本文件进行登录(我知道我知道使用文本文件进行练习并不安全)
namespace text_file_login
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
private void btn_register_Click(object sender, EventArgs e)
{
StreamWriter user_reg =
new StreamWriter(@"E:\SSD\Controled Assesment\text file login\text file login\username.txt");
StreamWriter pass_reg =
new StreamWriter(@"E:\SSD\Controled Assesment\text file login\text file login\password.txt");
user_reg.WriteLine(txt_user.Text);
user_reg.Close();
pass_reg.WriteLine(txt_pass.Text);
pass_reg.Close();
MessageBox.Show("Register was sucessful");
}
private void btn_login_Click(object sender, EventArgs e)
{
string user = "";
string pass = "";
int user_line_number = 0;
int pass_line_number = 0;
StreamReader user_login =
new StreamReader(@"E:\SSD\Controled Assesment\text file login\text file login\username.txt");
StreamReader pass_login =
new StreamReader(@"E:\SSD\Controled Assesment\text file login\text file login\password.txt");
do
{
user = user_login.ReadLine();
user_line_number++;
if (user == txt_user.Text)
{
do
{
pass = pass_login.ReadLine();
pass_line_number++;
if ((pass == txt_pass.Text) && (user_line_number == pass_line_number))
{
MessageBox.Show("Login was succesful");
//this.Close();
}
else
{
if ((user_line_number > pass_line_number) || (user_line_number < pass_line_number))
{
MessageBox.Show("Wrong Password");
}
}
} while (pass_login.Peek() != -1);
}
} while (user_login.Peek() != -1);
}
}
}
登录成功,注册成功。但是,如果用户名或密码错误,我很确定这将永远循环。我如何结束循环,以便在用户名或密码错误时显示消息框? 我不太关心多个用户名,但是如果你可以解决这个问题?
抱歉,如果写得不好。我是初学者
如果你想 "break" 跳出循环,你真的可以这样做:
do {
if (something)
break;
}
while (condition)