只是有点迷失在这里。我的问题是我编写了一个简单的代码块,允许用户选择一种水果。代码做了我想做的事情,除了一个问题,我需要它循环回到代码的开头,以便用户重新选择一个可选的水果。
但是,我似乎无法让它工作,但我怀疑问题在于我对循环的理解需要更多关注。我知道我需要一个初始化程序和某种条件来执行循环。我尝试了很多(疯狂的)事情,包括尝试像这样执行循环:for (string[ ] fruitArray = {"Banana", "Apple", "Orange", "Pineapple"}); 但是编译器没有它,我想堆栈溢出社区也不会接受。任何帮助和建议表示赞赏。
{
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();
}
}
}