C# 我正在使用字符串数组,我希望使用“foreach”来显示它们的列表

本文关键字:显示 列表 foreach 我希望 字符串 数组 | 更新日期: 2023-09-27 18:33:07

我相信除了foreach语句之外,我的大部分代码大部分都是正确的。但是,由于程序未编译,我无法对其进行测试。 我将不胜感激任何和所有的帮助,即使我的结构需要有所改变。

private static void PickScreen(Human myHuman)
    {
        var tries = 3;
        bool answer = false;
        string choice= "";
        string[] choices = new string[6];

        choices[0] = "Name";
        choices[1] = "Age";
        choices[2] = "Scar Type";
        choices[3] = "Weapon";
        choices[4] = "Hero Status";
        choices[5] = "Potions";
        DisplayNewScreenHeader();
        Console.WriteLine(" What screen would you like to go to?");
        Console.WriteLine();

        foreach (string i in choices)
        {
            Console.WriteLine(" Screen Choice: {0}", choices[]);
        }
        Console.WriteLine();
        Console.WriteLine(" Or you can type in " + "'"Default'"" + "to not have to do any of the query screens and have your character have default settings.");
        while(tries > 0)
        {
             Console.WriteLine(" Choice: ");
             choice = Console.ReadLine();
              if (choice.Equals("name", StringComparison.OrdinalIgnoreCase))
                 {
                        DisplayGetHeroName(myHuman);
                    answer = true;
                 }
              else if (choice.Trim().Equals("age", StringComparison.OrdinalIgnoreCase))
                 {
                        DisplayGetUsersAge(myHuman);
                    answer = true;
                 }
              else if (choice.Trim().Equals("Scar Type", StringComparison.OrdinalIgnoreCase))
                {
                        DisplayGetScar(myHuman);
                 answer = true;
             }
             else if (choice.Trim().Equals("weapon", StringComparison.OrdinalIgnoreCase))
             {
                        DisplayGetWeapon(myHuman);
                 answer = true;
             }
              else if (choice.Trim().Equals("Hero Status", StringComparison.OrdinalIgnoreCase))
             {
                        DisplayGetHeroStatus(myHuman);
                    answer = true;
             }
             else if (choice.Trim().Equals("Potions", StringComparison.OrdinalIgnoreCase))
             {
                        DisplayAddBackpackItems(myHuman);
                    answer = true;
             }
              else if (choice.Trim().Equals("Default", StringComparison.OrdinalIgnoreCase))
              {
                  Console.WriteLine(" Looks like you went with the lazy way.  Anyways go conquer basements, and become ruler of stuff!");
              }
             else 
             {
                 Console.WriteLine(" Dog gone it, yer Missed up a bit tad.");
                 Console.WriteLine();
                 Console.WriteLine(" Try again");
                 tries -= 1;
             }
        }
       if (answer == false)
         {
           DisplayNewScreenHeader();
           Console.WriteLine(" Since you're incompetent you no longer have the option to pick your query screens. They will simply go in order now.");
           DisplayGetHeroName(myHuman);
           DisplayGetUsersAge(myHuman);
           DisplayGetScar(myHuman);
           DisplayGetWeapon(myHuman);
           DisplayGetHeroStatus(myHuman);
           DisplayAddBackpackItems(myHuman);
           DisplayReturnPrompt();
         }

    }

C# 我正在使用字符串数组,我希望使用“foreach”来显示它们的列表

foreach 循环中,随着循环的继续,您的变量(在本例中为 i )设置为枚举中的每个值。 您需要更改:

foreach (string i in choices)
        {
            Console.WriteLine(" Screen Choice: {0}", choices[]);
        }

自:

foreach (string i in choices)
        {
            Console.WriteLine(" Screen Choice: {0}", i);
        }

更改

Console.WriteLine(" Screen Choice: {0}", choices[]);

Console.WriteLine(" Screen Choice: {0}", i);

您的foreach循环应如下所示,因为i实际上是choices中的条目之一

foreach (string i in choices)
    {
        Console.WriteLine(" Screen Choice: {0}", i);
    }

foreach遍历数组时,i将具有以下值:

choices[1]
choices[2]
choices[3]
.......
etc