In: Computer Science
Create a console app c#. Using a List, ask the user to enter all of their favorite things. Once they
are done, randomly pick a value from the list and display it.
Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ListDemo
{
class Program
{
static void Main(string[] args)
{
IList<string> thingsList = new List<string>();
Random rand = new Random();
Console.Write("\nHow many favourite things do you want to store
into the List: ");
int num = Convert.ToInt32(Console.ReadLine());
for (int i = 1; i <= num; i++)
{
Console.Write("Enter thing/value"+i+": ");
string thing = Console.ReadLine();
thingsList.Add(thing);
}
Console.WriteLine("\nList of favourite things: \n");
foreach (string s1 in thingsList)
{
Console.WriteLine(s1);
}
int selectItem = rand.Next(thingsList.Count ); //randomly selecting a index number
Console.WriteLine("\n\nRandomly selected value : "+thingsList[selectItem ]);
Console.ReadKey();
}
}
}
Output: