简体   繁体   English

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

[英]C# I'm using string arrays, and I was hoping to use a “foreach” to Display a list of them

I believe that most of my code is correct for the most part aside from the foreach statement. 我相信除了foreach语句外,我的大部分代码在大多数情况下都是正确的。 I haven't been able to test it however due to the fact that the program isn't compiling. 但是由于程序未编译,我无法对其进行测试。 I'd appreciate any and all help, even if my structure needs to change somewhat. 我将不胜感激,即使我的结构需要进行一些更改,我也将不胜感激。

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();
         }


    }

Within a foreach loop, your variable (in this case i ) is set to each value in the enumeration as the loop continues. foreach循环中,随着循环的继续,您的变量(在本例中为i )被设置为枚举中的每个值。 You'll need to change: 您需要更改:

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

        }

To: 至:

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

        }

Your foreach loop should as below as i is actually one of the entries in choices 您的foreach循环应如下所示,因为i实际上是choices项之一

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

    }

As foreach iterates through your array, i will have the values: 至于foreach遍历您的数组, i将有值:

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

Change 更改

Console.WriteLine(" Screen Choice: {0}", choices[]); Console.WriteLine(“屏幕选择:{0}”,options []);

To

Console.WriteLine(" Screen Choice: {0}", i); Console.WriteLine(“屏幕选择:{0}”,i);

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

相关问题 如何在C#中初始化字符串数组列表? - How do I initialize a list of string arrays in C#? c# 项目? 我应该使用字符串 arrays 吗? (这甚至是一件事吗?!) - A c# project! should i use String arrays? (is that even a thing?!) 如何使用C#在一个foreach循环中使用两个数组 - How to use two arrays in one foreach loop using C# 如何根据其中一个按字母顺序对 4 个单独的字符串数组(具有相同的长度)进行排序? (在 C# 中) - How can I sort 4 seperate string arrays (with the same lenght), according to one of them in alphabetical order? (in C#) C#-数组-输入名称并显示它们 - C# - Arrays - Enter Names and display them 什么时候应该使用数组列表(即列表) <string []> =新清单 <string []> ())在C#中? - When should I use List of Array (i.e. List<string []> = new List<string []>()) in C#? 我需要在字符串中查找特殊字符并将其显示在C#的标签中 - I need to find Special Characters in a string and display them in a label in C# c#当我尝试在我的列表中调试console.write元素时,如何修复foreach <string> - c# how to fix foreach , when i try to console.write elements in my list<string> c#和列表:如何使用foreach循环从列表中检索对象? - c# and Lists: How do I use the foreach loop to retrieve the objects form my list? C# 对象和数组,如何使用它们? - C# objects and arrays, how to use them?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM