开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜