c# dictionary foreach loop, how to make it loop if the user input is not in the dictionary
Okay I have this code.
while (true)
{
string[] items = Console.ReadLine().Split(' ');
string result = null;
foreach (string itemNumber in items)
{
if (tdItems.ContainsKey(itemNumber))
{
result += " + " + tdItems[itemNumber];
}
else
{
Console.WriteLine("You have entered a drop which is not in the database, Try again");
continue;
}
}
WriteToTextFile(result.Substring(3) + Environment.NewLine);
}
Basically, I've put an If command in to make sure that when the user puts in s开发者_运维百科ay 1 2 3 or whatever, it works and if he puts like 21 99 which is not in the dictionary then it will say a line and go back to the loop. However it keeps on erroring out?
Why is it doing this, just a bit confused.
result is null if the user doesn't enter anything that's in the database
result.Substring(3) causes the exception.
精彩评论