Just a bit lost here. My issue is that I have written a simple block of code which allows a user to select a fruit. The code does what I want it to do bar one issue, I need it to loop back to the start of the code in order for the user to re-select one of the optional fruit.
However, I can't seem to get it working but I suspect the problem is my understanding of loops which will require more attention. I understand I would need an initializer and a condition of some kind for the loop to execute. I have tried many (crazy) things including trying to execute the loop like this : for (string[ ] fruitArray = {"Banana", "Apple", "Orange", "Pineapple"}); but the compiler is having none of it and I imagine the Stack overflow community wouldn't accept that either. Any help and advice is appreciated.
{
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();
}
}
}
foreach (..with that stringforthen pressing tab? It should give you the skeleton of the proper syntax.