我正在制作一个密码生成器,我有三个复选框:小写字母、大写字母和数字。现在我有一个 IF 声明说:
IF (check1.checked == true & check2.checked == true & check3.checked == true)
{
length = Convert.ToInt32(lengthTextBox.Text);
string password = "";
int choice;
Generate gen = new Generate();
for (int i = 1; i <= length; i++)
{
choice = rnd.Next(1, 4);
if (choice == 1)
{
password = password + gen.lower();
}
else if (choice == 2)
{
password = password + gen.upper();
}
else if (choice == 3)
{
password = password + gen.number();
}
}
passwordTextBox.Text = password;
}
Else IF (check1.checked == true & check2.checked == true)
{
length = Convert.ToInt32(lengthTextBox.Text);
string password = "";
int choice;
Generate gen = new Generate();
for (int i = 1; i <= length; i++)
{
choice = rnd.Next(1, 3);
if (choice == 1)
{
password = password + gen.lower();
}
else if (choice == 2)
{
password = password + gen.upper();
}
}
passwordTextBox.Text = password;
}
等等。这种方法效率极低。我在制作收银机程序时遇到了这个问题(10种浇头选择,检查你想要的,程序将价格加在一起。如果你取消选中一个浇头,价格将减去那个数量。),所以我只是放弃了。但现在,我决心找到一种隐喻购物的方式。