我想制作一个随机“密码”生成器,生成不同长度的字母。您输入长度,它会生成长度随机字符。我还希望将一些随机字母大写。我有一个变量 rndCap,它是 1 或 2。如果 rndCap == 2,if 将把它变成一个大写字母,如果不是,它什么也不做。答案是所有字符组合在一起。当我执行时,数字都是小写的。一切正常,除了它随机大写一个字符。
Random rnd = new Random();
string[] alphabet = new string[27];
alphabet[1] = "a";
alphabet[2] = "b";
alphabet[3] = "c";
alphabet[4] = "d";
alphabet[5] = "e";
alphabet[6] = "f";
alphabet[7] = "g";
alphabet[8] = "h";
alphabet[9] = "i";
alphabet[10] = "j";
alphabet[11] = "k";
alphabet[12] = "l";
alphabet[13] = "m";
alphabet[14] = "n";
alphabet[15] = "o";
alphabet[16] = "p";
alphabet[17] = "q";
alphabet[18] = "r";
alphabet[19] = "s";
alphabet[20] = "t";
alphabet[21] = "u";
alphabet[22] = "v";
alphabet[23] = "w";
alphabet[24] = "x";
alphabet[25] = "y";
alphabet[26] = "z";
string answer = "";
int length = Convert.ToInt32(lengthTextBox.Text);
int rndCap;
int rndLetter;
for (int i = 1; i <= length; i++)
{
rndCap = rnd.Next(1, 3);
rndLetter = rnd.Next(1, 27);
string tempMem = alphabet[rndLetter];
if (rndCap == 2)
{
tempMem.ToUpper();
}
answer = answer + tempMem;
}
passwordTextBox.Text = answer;