Home > database >  Empty value output from string array
Empty value output from string array

Time:10-08

I practice with C# Console Application. I have little problem with no value output from string array, when I insert a value from typing on console application then and there are no display from favorite = { food }. let me show you simple code lines, see below:

string food = "";
string[] favorite = { food };

Console.WriteLine("what is your favorite food?");
food = Console.ReadLine();

Console.WriteLine("Your favorite food is" favorite[0]);

CodePudding user response:

Collect 5 favorite foods

string[] favourite = new string[5];

for (int i = 1; i <= 5; i  )
{
    Console.WriteLine($"what is your #{i} favourite food?");
    favourite[i - 1] = Console.ReadLine();
}


Console.WriteLine("Here are your top foods --");
foreach(string f in favourite)
    Console.WriteLine($"--- {f}");

  • Related