An example of how you could do this is:
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
bool read = true;
List<int> list = new List<int>();
do{
string input = Console.ReadLine();
int x = 0;
if(input == "start")
{
read = false;
}
else if(int.TryParse(input, out x))
{
list.Add(x);
}
else
{
Console.WriteLine("Invalid Input");
}
} while(read);
foreach(var z in list)
{
Console.WriteLine(z);
}
}
}
You will have to adjust this to your needs, but this is how you could handle it.
Notice the List<>
it is dynamically growing and shrinking. That way you don’t have to specify the amount of numbers you want to add. That way you can just add as long as you wish till you give the Start
command.
3
solved How to use unassigned local variable? [closed]