带有数据验证的C#列表计数

时间:2016-04-02 17:08:38

标签: c# count outofrangeexception

感谢您的反馈。我还有一个超出范围的例外。我正在调试,但无法理解为什么我会收到异常。如果int高于某个其他int,它应该工作。它与length.Count有什么关系吗?

更新代码

                    do
                {
                    Console.Write("Input the element that you want to exclude from the list: ");
                    result = int.TryParse(Console.ReadLine(), out delSelection);

                    tempInputDelInt = (int)(tempList[delSelection]);
                    tempInputDelStr = Convert.ToString(tempInputDelInt);

                    if (result == true && tempInputDelInt >= 0 && tempInputDelInt < tempList.Count)
                    {
                        tempList.RemoveAt(delSelection);
                        Console.WriteLine("\nYou've deleted the temperature " + tempInputDelStr + " from index " + delSelection);
                        success = false;
                        Console.ReadKey(true);
                        Console.Clear();
                    }

                    else if (result == true && tempInputDelInt >= tempList.Count || tempInputDelInt < 0)
                    {
                        Console.WriteLine("You've input a number that's outside the list. Input a digit between 0 and " + (tempList.Count - 1) + ".\n");
                        success = true;
                        Console.ReadKey(true);
                    }

                    else if (result == false)
                    {
                        Console.WriteLine("\nYou didn't input a digit, try again!\n");
                        success = true;
                        Console.ReadKey(true);
                    }
                } while (success);
                success = false;

我在验证第一个if语句时遇到了一些问题。我希望在输入数组之外的值时捕获这些情况,但我不能设法。

我已经使用array.Length成功完成了一个几乎完全相同的程序。

array.List和list.Count之间有什么区别吗?这是问题所在吗?

提前感谢您的帮助!

                    do
                {
                    Console.Write("Input the element that you want to exclude from the list: ");
                    result = int.TryParse(Console.ReadLine(), out delSelection);


                    tempInputDel = Convert.ToString(tempList[delSelection]);

                    if (result == true && delSelection >= 0 && delSelection < tempList.Count)
                    {
                        tempList.RemoveAt(delSelection);
                        Console.WriteLine("\nYou've deleted the temperature " + tempInputDel + " from index " + delSelection);
                        Console.ReadKey(true);
                        Console.Clear();
                    }

                    else if (result == true && delSelection >= tempList.Count || delSelection < 0)
                    {
                        Console.WriteLine("You've input a number that's outside the list. Input a digit between 0 and " + (tempList.Count - 1) + ".\n");

                    }

                    else if (result == false)
                    {
                        Console.WriteLine("\nYou didn't input a digit, try again!");
                        Console.ReadKey(true);
                    }
                } while (result == false);

2 个答案:

答案 0 :(得分:1)

更改此

tempInputDelInt = (int)(tempList[delSelection]);

到这个

tempInputDelInt = (int)delSelection;

无论用户输入什么号码,您都可以将其用作索引来确定要删除的项目。因此,它不是您从tempList获得的价值。

答案 1 :(得分:0)

我假设你的超范围异常发生在这里:

tempInputDel = Convert.ToString(tempList[delSelection]);

如果delSelectiontempList范围内,则在检查之前无法执行此操作。将其移到第一个if块内。

一般建议:

您可以使用调试器逐步从语句转到语句,以找到(例如)抛出异常的确切源代码位置。