繁体   English   中英

随机目录生成器使用数字,但当我包含字母并且没有错误时不会创建任何内容

[英]Random Directory Generator Working With numbers but won't create anything when I include letters and i get no errors

for (int i = 0; i < WindowForm.GalaxySize; i++)
            {
                string CGenSystem;

                string A1 = rnd.Next(1, 3).ToString();
                string A2 = rnd.Next(1, 3).ToString();

                string B1 = rnd.Next(1, 3).ToString();
                string B2 = rnd.Next(1, 3).ToString();

                string C1 = rnd.Next(1, 3).ToString();
                string C2 = rnd.Next(1, 3).ToString();

                if(Int32.Parse(A1) == 1)
                {
                    A1 = rnd.Next(0, 10).ToString();
                }
                else
                {
                    A1 = rnd.Next(1, 27).ToString();

                    if (Int32.Parse(A1) == 1) { A1 = "A"; return; }
                    if (Int32.Parse(A1) == 2) { A1 = "B"; return; }
                    if (Int32.Parse(A1) == 3) { A1 = "C"; return; }
                    if (Int32.Parse(A1) == 4) { A1 = "D"; return; }
                    if (Int32.Parse(A1) == 5) { A1 = "E"; return; }
                    if (Int32.Parse(A1) == 6) { A1 = "F"; return; }
                    if (Int32.Parse(A1) == 7) { A1 = "G"; return; }
                    if (Int32.Parse(A1) == 8) { A1 = "H"; return; }
                    if (Int32.Parse(A1) == 9) { A1 = "I"; return; }
                    if (Int32.Parse(A1) == 10) { A1 = "J"; return; }
                    if (Int32.Parse(A1) == 11) { A1 = "K"; return; }
                    if (Int32.Parse(A1) == 12) { A1 = "L"; return; }
                    if (Int32.Parse(A1) == 13) { A1 = "M"; return; }
                    if (Int32.Parse(A1) == 14) { A1 = "N"; return; }
                    if (Int32.Parse(A1) == 15) { A1 = "O"; return; }
                    if (Int32.Parse(A1) == 16) { A1 = "P"; return; }
                    if (Int32.Parse(A1) == 17) { A1 = "Q"; return; }
                    if (Int32.Parse(A1) == 18) { A1 = "R"; return; }
                    if (Int32.Parse(A1) == 19) { A1 = "S"; return; }
                    if (Int32.Parse(A1) == 20) { A1 = "T"; return; }
                    if (Int32.Parse(A1) == 21) { A1 = "U"; return; }
                    if (Int32.Parse(A1) == 22) { A1 = "V"; return; }
                    if (Int32.Parse(A1) == 23) { A1 = "W"; return; }
                    if (Int32.Parse(A1) == 24) { A1 = "X"; return; }
                    if (Int32.Parse(A1) == 25) { A1 = "Y"; return; }
                    if (Int32.Parse(A1) == 26) { A1 = "Z"; }
                }

                if (Int32.Parse(A2) == 1)
                {
                    A2 = rnd.Next(0, 10).ToString();
                }
                else
                {
                    A2 = rnd.Next(1, 27).ToString();

                    if (Int32.Parse(A2) == 1) { A2 = "A"; return; }
                    if (Int32.Parse(A2) == 2) { A2 = "B"; return; }
                    if (Int32.Parse(A2) == 3) { A2 = "C"; return; }
                    if (Int32.Parse(A2) == 4) { A2 = "D"; return; }
                    if (Int32.Parse(A2) == 5) { A2 = "E"; return; }
                    if (Int32.Parse(A2) == 6) { A2 = "F"; return; }
                    if (Int32.Parse(A2) == 7) { A2 = "G"; return; }
                    if (Int32.Parse(A2) == 8) { A2 = "H"; return; }
                    if (Int32.Parse(A2) == 9) { A2 = "I"; return; }
                    if (Int32.Parse(A2) == 10) { A2 = "J"; return; }
                    if (Int32.Parse(A2) == 11) { A2 = "K"; return; }
                    if (Int32.Parse(A2) == 12) { A2 = "L"; return; }
                    if (Int32.Parse(A2) == 13) { A2 = "M"; return; }
                    if (Int32.Parse(A2) == 14) { A2 = "N"; return; }
                    if (Int32.Parse(A2) == 15) { A2 = "O"; return; }
                    if (Int32.Parse(A2) == 16) { A2 = "P"; return; }
                    if (Int32.Parse(A2) == 17) { A2 = "Q"; return; }
                    if (Int32.Parse(A2) == 18) { A2 = "R"; return; }
                    if (Int32.Parse(A2) == 19) { A2 = "S"; return; }
                    if (Int32.Parse(A2) == 20) { A2 = "T"; return; }
                    if (Int32.Parse(A2) == 21) { A2 = "U"; return; }
                    if (Int32.Parse(A2) == 22) { A2 = "V"; return; }
                    if (Int32.Parse(A2) == 23) { A2 = "W"; return; }
                    if (Int32.Parse(A2) == 24) { A2 = "X"; return; }
                    if (Int32.Parse(A2) == 25) { A2 = "Y"; return; }
                    if (Int32.Parse(A2) == 26) { A2 = "Z"; return; }
                }

                if (Int32.Parse(B1) == 1)
                {
                    B1 = rnd.Next(0, 10).ToString();
                }
                else
                {
                    B1 = rnd.Next(1, 27).ToString();

                    if (Int32.Parse(B1) == 1) { B1 = "A"; return; }
                    if (Int32.Parse(B1) == 2) { B1 = "B"; return; }
                    if (Int32.Parse(B1) == 3) { B1 = "C"; return; }
                    if (Int32.Parse(B1) == 4) { B1 = "D"; return; }
                    if (Int32.Parse(B1) == 5) { B1 = "E"; return; }
                    if (Int32.Parse(B1) == 6) { B1 = "F"; return; }
                    if (Int32.Parse(B1) == 7) { B1 = "G"; return; }
                    if (Int32.Parse(B1) == 8) { B1 = "H"; return; }
                    if (Int32.Parse(B1) == 9) { B1 = "I"; return; }
                    if (Int32.Parse(B1) == 10) { B1 = "J"; return; }
                    if (Int32.Parse(B1) == 11) { B1 = "K"; return; }
                    if (Int32.Parse(B1) == 12) { B1 = "L"; return; }
                    if (Int32.Parse(B1) == 13) { B1 = "M"; return; }
                    if (Int32.Parse(B1) == 14) { B1 = "N"; return; }
                    if (Int32.Parse(B1) == 15) { B1 = "O"; return; }
                    if (Int32.Parse(B1) == 16) { B1 = "P"; return; }
                    if (Int32.Parse(B1) == 17) { B1 = "Q"; return; }
                    if (Int32.Parse(B1) == 18) { B1 = "R"; return; }
                    if (Int32.Parse(B1) == 19) { B1 = "S"; return; }
                    if (Int32.Parse(B1) == 20) { B1 = "T"; return; }
                    if (Int32.Parse(B1) == 21) { B1 = "U"; return; }
                    if (Int32.Parse(B1) == 22) { B1 = "V"; return; }
                    if (Int32.Parse(B1) == 23) { B1 = "W"; return; }
                    if (Int32.Parse(B1) == 24) { B1 = "X"; return; }
                    if (Int32.Parse(B1) == 25) { B1 = "Y"; return; }
                    if (Int32.Parse(B1) == 26) { B1 = "Z"; return; }
                }

                if (Int32.Parse(B2) == 1)
                {
                    B2 = rnd.Next(0, 10).ToString();
                }
                else
                {
                    B2 = rnd.Next(1, 27).ToString();

                    if (Int32.Parse(B2) == 1) { B2 = "A"; return; }
                    if (Int32.Parse(B2) == 2) { B2 = "B"; return; }
                    if (Int32.Parse(B2) == 3) { B2 = "C"; return; }
                    if (Int32.Parse(B2) == 4) { B2 = "D"; return; }
                    if (Int32.Parse(B2) == 5) { B2 = "E"; return; }
                    if (Int32.Parse(B2) == 6) { B2 = "F"; return; }
                    if (Int32.Parse(B2) == 7) { B2 = "G"; return; }
                    if (Int32.Parse(B2) == 8) { B2 = "H"; return; }
                    if (Int32.Parse(B2) == 9) { B2 = "I"; return; }
                    if (Int32.Parse(B2) == 10) { B2 = "J"; return; }
                    if (Int32.Parse(B2) == 11) { B2 = "K"; return; }
                    if (Int32.Parse(B2) == 12) { B2 = "L"; return; }
                    if (Int32.Parse(B2) == 13) { B2 = "M"; return; }
                    if (Int32.Parse(B2) == 14) { B2 = "N"; return; }
                    if (Int32.Parse(B2) == 15) { B2 = "O"; return; }
                    if (Int32.Parse(B2) == 16) { B2 = "P"; return; }
                    if (Int32.Parse(B2) == 17) { B2 = "Q"; return; }
                    if (Int32.Parse(B2) == 18) { B2 = "R"; return; }
                    if (Int32.Parse(B2) == 19) { B2 = "S"; return; }
                    if (Int32.Parse(B2) == 20) { B2 = "T"; return; }
                    if (Int32.Parse(B2) == 21) { B2 = "U"; return; }
                    if (Int32.Parse(B2) == 22) { B2 = "V"; return; }
                    if (Int32.Parse(B2) == 23) { B2 = "W"; return; }
                    if (Int32.Parse(B2) == 24) { B2 = "X"; return; }
                    if (Int32.Parse(B2) == 25) { B2 = "Y"; return; }
                    if (Int32.Parse(B2) == 26) { B2 = "Z"; return; }
                }

                if (Int32.Parse(C1) == 1)
                {
                    C1 = rnd.Next(0, 10).ToString();
                }
                else
                {
                    C1 = rnd.Next(1, 27).ToString();

                    if (Int32.Parse(C1) == 1) { C1 = "A"; return; }
                    if (Int32.Parse(C1) == 2) { C1 = "B"; return; }
                    if (Int32.Parse(C1) == 3) { C1 = "C"; return; }
                    if (Int32.Parse(C1) == 4) { C1 = "D"; return; }
                    if (Int32.Parse(C1) == 5) { C1 = "E"; return; }
                    if (Int32.Parse(C1) == 6) { C1 = "F"; return; }
                    if (Int32.Parse(C1) == 7) { C1 = "G"; return; }
                    if (Int32.Parse(C1) == 8) { C1 = "H"; return; }
                    if (Int32.Parse(C1) == 9) { C1 = "I"; return; }
                    if (Int32.Parse(C1) == 10) { C1 = "J"; return; }
                    if (Int32.Parse(C1) == 11) { C1 = "K"; return; }
                    if (Int32.Parse(C1) == 12) { C1 = "L"; return; }
                    if (Int32.Parse(C1) == 13) { C1 = "M"; return; }
                    if (Int32.Parse(C1) == 14) { C1 = "N"; return; }
                    if (Int32.Parse(C1) == 15) { C1 = "O"; return; }
                    if (Int32.Parse(C1) == 16) { C1 = "P"; return; }
                    if (Int32.Parse(C1) == 17) { C1 = "Q"; return; }
                    if (Int32.Parse(C1) == 18) { C1 = "R"; return; }
                    if (Int32.Parse(C1) == 19) { C1 = "S"; return; }
                    if (Int32.Parse(C1) == 20) { C1 = "T"; return; }
                    if (Int32.Parse(C1) == 21) { C1 = "U"; return; }
                    if (Int32.Parse(C1) == 22) { C1 = "V"; return; }
                    if (Int32.Parse(C1) == 23) { C1 = "W"; return; }
                    if (Int32.Parse(C1) == 24) { C1 = "X"; return; }
                    if (Int32.Parse(C1) == 25) { C1 = "Y"; return; }
                    if (Int32.Parse(C1) == 26) { C1 = "Z"; return; }
                }

                if (Int32.Parse(C2) == 1)
                {
                    C2 = rnd.Next(0, 10).ToString();
                }
                else
                {
                    C2 = rnd.Next(1, 27).ToString();

                    if (Int32.Parse(C2) == 1) { C2 = "A"; return; }
                    if (Int32.Parse(C2) == 2) { C2 = "B"; return; }
                    if (Int32.Parse(C2) == 3) { C2 = "C"; return; }
                    if (Int32.Parse(C2) == 4) { C2 = "D"; return; }
                    if (Int32.Parse(C2) == 5) { C2 = "E"; return; }
                    if (Int32.Parse(C2) == 6) { C2 = "F"; return; }
                    if (Int32.Parse(C2) == 7) { C2 = "G"; return; }
                    if (Int32.Parse(C2) == 8) { C2 = "H"; return; }
                    if (Int32.Parse(C2) == 9) { C2 = "I"; return; }
                    if (Int32.Parse(C2) == 10) { C2 = "J"; return; }
                    if (Int32.Parse(C2) == 11) { C2 = "K"; return; }
                    if (Int32.Parse(C2) == 12) { C2 = "L"; return; }
                    if (Int32.Parse(C2) == 13) { C2 = "M"; return; }
                    if (Int32.Parse(C2) == 14) { C2 = "N"; return; }
                    if (Int32.Parse(C2) == 15) { C2 = "O"; return; }
                    if (Int32.Parse(C2) == 16) { C2 = "P"; return; }
                    if (Int32.Parse(C2) == 17) { C2 = "Q"; return; }
                    if (Int32.Parse(C2) == 18) { C2 = "R"; return; }
                    if (Int32.Parse(C2) == 19) { C2 = "S"; return; }
                    if (Int32.Parse(C2) == 20) { C2 = "T"; return; }
                    if (Int32.Parse(C2) == 21) { C2 = "U"; return; }
                    if (Int32.Parse(C2) == 22) { C2 = "V"; return; }
                    if (Int32.Parse(C2) == 23) { C2 = "W"; return; }
                    if (Int32.Parse(C2) == 24) { C2 = "X"; return; }
                    if (Int32.Parse(C2) == 25) { C2 = "Y"; return; }
                    if (Int32.Parse(C2) == 26) { C2 = "Z"; return; }
                }

                CGenSystem = A1+A2+"-"+B1+B2+"-"+C1+C2; ;

                if(!Directory.Exists(@"GameData\\GameSaves\\"+WindowForm.GalaxyName+"\\GalaxyData\\"+CGenSystem))
                {
                    Directory.CreateDirectory(@"GameData\\GameSaves\\" + WindowForm.GalaxyName + "\\GalaxyData\\" + CGenSystem);
                }
            }

所以我一直在编写一个随机生成星系的游戏,当将前 6 个字符串设置为等于 rnd.next(1,2).ToString(); 时,星系统名称由下面的代码确定。 我将所有系统都生成为数字,这些系统都可以正常工作,我不打算将它们作为唯一的数字,它们有 50/50 的机会成为字母,出于某种原因,当我修复它以便它执行此操作时,现在当它开始生成时,它不会在 GalaxyData 下创建任何文件夹,并且绝对没有检测到错误。 这真的让我很困惑,我编码错了还是 C# 有问题?

即使您的实际问题显然在其他地方,我也想向您展示如何大大简化您的程序:

// create an array of all your random digits and characters
// ... if you want to put a higher chance on any, just add them multiple times
char[] alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray(); //***

for (int i = 0; i < WindowForm.GalaxySize; i++)
{
    // define an array to contain the parts of your random name
    string[] nameparts = new string[6];

    // loop as many times as necessary to fill your nameparts array
    for (int j = 0; j < nameparts.Length; j++)
    {
        // pick a random character from your alphanum array
        int c = rnd.Next(0, alphanum.Length - 1);

        // ... and store it in the nameparts array
        nameparts[j] = alphanum[c].ToString();
    }

    // combine the name parts in the desired format
    string CGenSystem = string.Format("{0}{1}-{2}{3}-{4}{5}", nameparts);

    // Use System.IO.Path.Combine to handle creating path strings
    string path = Path.Combine(@"C:\Galactic Field\bin\Debug\GameData\GameSaves", WindowForm.GalaxyName, "GalaxyData", CGenSystem);
}

***) 由在字母表生成字母数组提供

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM