コードは次のとおりです(passwordLengthBoxはNumericUpDownボックス、rとkは乱数です)
private void generateButton_Click(object sender, EventArgs e)
{
int r, k;
int passwordLength = (Int32)passwordLengthBox.Value;
string password = "";
char[] upperCase = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
char[] lowerCase = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
int[] numbers = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
Random rRandom = new Random();
for (int i = 0; i < passwordLength; i++)
{
r = rRandom.Next(3);
if (r == 0)
{
k = rRandom.Next(0, 25);
password += upperCase[k];
}
else if (r == 1)
{
k = rRandom.Next(0, 25);
password += lowerCase[k];
}
else if (r == 2)
{
k = rRandom.Next(0, 9);
password += numbers[k];
}
}
textBox.Text = password;
}
このプログラムは、文字(大文字と小文字の両方)と数字を使用して、選択した長さのランダムなパスワードを作成します。問題は、プログラムが私が選択したパスワードの長さを作成しないことです。
たとえば、パスワードの長さを設定するNumericUpDownボックス(passwordLengthBox)に5と入力すると、5文字の長さのパスワードが表示される場合と、6/7/8文字の長さのパスワードが表示される場合があります。
私の間違いは何ですか?