这是代码:(passwordLengthBox是NumericUpDown Box,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 Box(passwordLengthBox)中键入5,设置密码长度,有时它会给我5个字符长的密码,有时6/7/8个字符长密码.
我的错是什么?