C#循环一段代码(包含一个数组)

时间:2012-10-31 02:09:27

标签: c# arrays loops

这里有点失落。我的问题是我编写了一个简单的代码块,允许用户选择水果。代码执行我想要它做的事情吧,我需要它循环回到代码的开头,以便用户重新选择一个可选的水果。

然而,我似乎无法让它工作但我怀疑问题是我对循环的理解需要更多关注。我知道我需要一个初始化器和某种条件来执行循环。我尝试了许多(疯狂的)事情,包括尝试执行这样的循环: for(string [] fruitArray = {“Banana”,“Apple”,“Orange”,“Pineapple”}); 但是编译器没有它,我想Stack溢出社区也不会接受它。任何帮助和建议表示赞赏。

{

            Console.WriteLine("\n", "\n");

            string[] fruitArray = {"Banana", "Apple", "Orange", "Pineapple"};

            Console.WriteLine("Please select your fruit: \n\n{0} \n{1} \n{2} \n{3}" + "\n", fruitArray[0], fruitArray[1], fruitArray[2], fruitArray[3]);
            string selection = Console.ReadLine();


            if (selection == fruitArray[0])
            {
                Console.WriteLine("\nYou have selected {0} ", fruitArray[0]);
            }
            else if (selection == fruitArray[1])
            {
                Console.WriteLine("\nYou have selected {0} ", fruitArray[1]);
            }
            else if (selection == fruitArray[2])
            {
                Console.WriteLine("\nYou have selected {0} ", fruitArray[2]);
            }
            else if (selection == fruitArray[3])
            {
                Console.WriteLine("\nYou have selected {0} ", fruitArray[3]);
            }
            else
            {
                Console.WriteLine("\nSelection not recognised. Please select fruit: ");
            }

         }

        Console.ReadKey();

    }
  }
}

4 个答案:

答案 0 :(得分:2)

无退出循环

while (true)
            {
                Console.WriteLine("\n", "\n");

                string[] fruitArray = { "Banana", "Apple", "Orange", "Pineapple" };

                Console.WriteLine("Please select your fruit: \n\n{0} \n{1} \n{2} \n{3}" + "\n", fruitArray[0], fruitArray[1],
                                  fruitArray[2], fruitArray[3]);
                string selection = Console.ReadLine();

                switch (selection)
                {
                    case "Banana":
                        Console.WriteLine("\nYou have selected {0} ", fruitArray[0]);
                        break;

                    case "Apple":
                        Console.WriteLine("\nYou have selected {0} ", fruitArray[1]);
                        break;

                    case "Orange":
                        Console.WriteLine("\nYou have selected {0} ", fruitArray[2]);
                        break;

                    case "Pineapple":
                        Console.WriteLine("\nYou have selected {0} ", fruitArray[3]);
                        break;                    
                } 
            }          

循环退出条件

            var isLoop = true;

            do
            {
                Console.WriteLine("\n", "\n");

                string[] fruitArray = {"Banana", "Apple", "Orange", "Pineapple"};

                Console.WriteLine("Please select your fruit: \n\n{0} \n{1} \n{2} \n{3}"   + "\n", fruitArray[0],
                                  fruitArray[1],
                                  fruitArray[2], fruitArray[3]);
                string selection = Console.ReadLine();


                switch (selection)
                {
                    case "Banana":
                        Console.WriteLine("\nYou have selected {0} ", fruitArray[0]);
                        break;

                    case "Apple":
                        Console.WriteLine("\nYou have selected {0} ", fruitArray[1]);
                        break;

                    case "Orange":
                        Console.WriteLine("\nYou have selected {0} ", fruitArray[2]);
                        break;

                    case "Pineapple":
                        Console.WriteLine("\nYou have selected {0} ", fruitArray[3]);
                        break;
                    default:
                    isLoop = false;
                    break;
                }
            } while (isLoop);

答案 1 :(得分:1)

C#For http://msdn.microsoft.com/en-us/library/ch45axte.aspx

C#foreach http://msdn.microsoft.com/en-us/library/ttw7t8t6.aspx

string[] fruitArray = { "Banana", "Apple", "Orange", "Pineapple" };

        Console.WriteLine("Please select your fruit: \n");            
        foreach (string fruit in fruitArray)
        {
            Console.Write("\n{0}", fruit);
        }

        Console.WriteLine();



            string selection = Console.ReadLine();
            for (int index = 0; index < fruitArray.Length; index++)
            {
                if (fruitArray[index].Equals(selection))
                {
                    Console.WriteLine("\nYou have selected {0} ", fruitArray[index]);
                    break;
                }
            }

            foreach (string fruit in new[] {"Banana", "Apple", "Orange", "Pineapple" })
            {
                if (fruit.Equals(selection))
                {
                    Console.WriteLine("\nYou have selected {0} ", fruit);
                    break;
                }
            }

答案 2 :(得分:1)

对于您的示例,这应该足够了:

string[] fruitArray = { "Banana", "Apple", "Orange", "Pineapple" };

Console.WriteLine("Please select your fruit: \n\n{0} \n{1} \n{2} \n{3}" + "\n", fruitArray[0], fruitArray[1],
                              fruitArray[2], fruitArray[3]);
string selection = Console.ReadLine();

Console.WriteLine("\nYou have selected {0} ", selection);

可能我错过了一些东西,我会很高兴明白这一点:)

此外,您可以通过迭代数组来改进第一条消息:

Console.WriteLine("Please select your fruit: \n");

for(int i=0;i<fruitArray.Length;i++)
{
    Console.WriteLine(fruitArray[i]);
}

希望它有所帮助。

答案 3 :(得分:1)

以下代码应该完全符合您的要求。不仅如此,它简化了甚至使用块if语句或switch的需要,简单的Array.FindIndex足以确定输入是否有效。另外,我添加了一个exit子句,这样用户可以在完成后退出应用程序并删除UI的任何混乱我在每次“迭代”后清除控制台窗口。

    static void Main(string[] args)
    {
        var selection = "";
        while (selection != "q")
        {
            Console.WriteLine(Environment.NewLine);
            string[] fruitArray = { "Banana", "Apple", "Orange", "Pineapple" };

            Console.WriteLine("Please select your fruit (or Q to quit): \n\n{0} \n{1} \n{2} \n{3}", fruitArray[0], fruitArray[1], fruitArray[2], fruitArray[3]);
            Console.Write(Environment.NewLine + "-> ");
            selection = Console.ReadLine().ToLower();

            // valid option selected
            var index = Array.FindIndex(fruitArray, (fruit) => fruit.ToLower() == selection);
            if (index > -1)
            {
                Console.Write("\nYou have selected {0}.", fruitArray[index]);
                ContinuePrompt();
            }
            else if (selection != "q")
            {
                Console.Write("\nSelection not recognised.");
                ContinuePrompt();
            }
        }
     }

    static void ContinuePrompt()
    {
        Console.Write(" Press any key to continue...");
        Console.ReadKey();
        Console.Clear();
    }