简体   繁体   中英

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. 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. 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 (string i in choices)
    {
        Console.WriteLine(" Screen Choice: {0}", i);

    }

As foreach iterates through your array, i will have the values:

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

Change

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

To

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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