Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 随机大写字母不工作_C# - Fatal编程技术网

C# 随机大写字母不工作

C# 随机大写字母不工作,c#,C#,我想制作一个随机的“密码”生成器,生成不同长度的字母。您输入长度,它使长度成为随机字符。我还想要一些随机字母大写。我有一个变量rndCap,它的值为1或2。如果rndCap==2,if会将其转换为大写字母,如果不是,则不执行任何操作。答案是所有字符的组合。当我执行时,数字都是小写的。除了随机将角色大写外,其他一切都可以正常工作 Random rnd = new Random(); string[] alphabet = new string[27]; alphabet[1] = "a"; a

我想制作一个随机的“密码”生成器,生成不同长度的字母。您输入长度,它使长度成为随机字符。我还想要一些随机字母大写。我有一个变量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;
Random rnd=new Random();
字符串[]字母表=新字符串[27];
字母表[1]=“a”;
字母表[2]=“b”;
字母表[3]=“c”;
字母表[4]=“d”;
字母表[5]=“e”;
字母[6]=“f”;
字母表[7]=“g”;
字母表[8]=“h”;
字母表[9]=“i”;
字母[10]=“j”;
字母表[11]=“k”;
字母[12]=“l”;
字母[13]=“m”;
字母表[14]=“n”;
字母表[15]=“o”;
字母[16]=“p”;
字母[17]=“q”;
字母[18]=“r”;
字母表[19]=“s”;
字母表[20]=“t”;
字母[21]=“u”;
字母表[22]=“v”;
字母[23]=“w”;
字母表[24]=“x”;
字母表[25]=“y”;
字母[26]=“z”;
字符串答案=”;
int length=Convert.ToInt32(lengthTextBox.Text);
int rndCap;
int rndLetter;

对于(inti=1;i我非常确定IF正在工作:)

但是,不会将手头的字符串更改为大写,而是将其作为结果返回。您必须指定该值。您想要的可能是:

tempMem = tempMem.ToUpper();
还有一条要遵循的规则:永远记住 编码、区域性和区域设置可能与当前的不同。

即使在这种情况下,我们只使用“无害的”ASCII字符,但要将其转化为大写字符串:

  • 使用指定区域性
  • 使用以使用“不变区域性”而不是默认区域性

正如Jeppe所指出的:在土耳其语语言环境中,i的大写字母与i不同,这会带来不必要的后果。

字符串是不可变的,因此需要重新分配它

tempMem = tempMem.ToUpper();
使用以下命令:

tempMem = tempMem.ToUpper();
由于
ToUpper()
不会将其调用的字符串设置为大写,因此它返回该字符串的副本,所有字母都大写。

ToUpper()
是一个返回大写字符串的方法,因此需要将tempMem的值指定给该值:

tempMem = tempMem.ToUpper();

调用ToUpper,但不指定值:

tempMem = tempMem.ToUpper()
也许这段代码通常更优雅:

    static void Main(string[] args)
    {
        Random cRandom = new Random(DateTime.Now.Millisecond);

        string answer = "";
        int length = Convert.ToInt32(6);
        int rndCap;
        int rndLetter;

        for (int i = 1; i <= length; i++)
        {

            rndCap = cRandom.Next(1, 3);

            rndLetter = cRandom.Next(0, 26);

            char tempMem = (char)('a' + rndLetter);
            string c2 = tempMem.ToString();
            if (rndCap == 2)
            {

               c2 = tempMem.ToString().ToUpper();

            }

            answer = answer + c2;

        }
        Console.WriteLine(answer);
        Console.ReadLine();
    }
static void Main(字符串[]args)
{
Random cRandom=新随机(DateTime.Now.毫秒);
字符串答案=”;
整数长度=转换为32(6);
int rndCap;
int rndLetter;

对于(inti=1;i我认为有更简单的方法来实现您想要实现的目标

        var lowerchar = "abcdefghijklmnopqrstuvwxyz";
        var upperchar = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        var numbers = "0123456789";
        var random = new Random();

        var result = new string(
            Enumerable.Repeat(lowerchar, 3)
                      .Select(s => s[random.Next(s.Length)])
                      .ToArray());

        result += new string(
            Enumerable.Repeat(upperchar, 3)
                      .Select(s => s[random.Next(s.Length)])
                      .ToArray());

         result += new string(
            Enumerable.Repeat(numbers, 2)
                      .Select(s => s[random.Next(s.Length)])
                      .ToArray());

        random = new Random();

        string password = new string(result.ToCharArray().OrderBy(s => random.Next().ToArray());

所有字符串操作都会返回新字符串,而不会修改原始字符串,因为字符串是可imutable的。
修改如下:tempMem=tempMem.ToUpper()

为什么不把大写字母放在你的字母表数组中?为什么你要创建一个大小为
27
的数组,然后从
1-26
索引?你应该创建一个大小为
26
的数组,然后从
0-25
索引。有人告诉我你需要复习一下基本知识……建议:做char[]array=“abcdefghijklmnopqrstuvwxyz”.ToCharArray();如果
不起作用,与发生简单错误的几率相比,
的几率有多大?:)不要使用标准随机密码生成!这是可预测的。String.toupper不变量()这可能会更好。@Dunbar yep,在处理本地化和编码时,默认值不起作用……举一个常见的例子,
“i”。ToUpper(new CultureInfo(“tr”)!=“i”
,这意味着在某些文化中,即“Turkish(Turkish)”“,在Ascii字符上说
ToUpper
会给出一个非Ascii字符。@JeppeStigNielsen很遗憾,我达到了每日评论上限,但还是感谢你的有用评论!更准确地说,
“I”。ToUpper(新文化信息(“tr tr”)==”İ“
”和
“İ”!=”I”
。请参阅。您的洗牌操作未正确完成。您选择的是随机布尔值,该值的熵不足以进行洗牌操作。您将以显著的冲突率结束(实际上是~n/2次冲突)由于这是一个稳定的排序,这意味着数字将更接近尾端,小写数字将更接近开始。相反,只需选择一个随机整数和顺序,这会降低碰撞的几率。你能给我一个例子说明你的意思或修改我的答案吗?而且,这可以进一步简化