C# 列表计数与数据验证
C# list count with data validation
感谢您的反馈。我仍然遇到超出范围的异常。我正在调试,但不明白为什么会出现异常。如果一个 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;
我在验证第一个 else 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);
我假设您的超出范围异常发生在这里:
tempInputDel = Convert.ToString(tempList[delSelection]);
如果 delSelection
在 tempList
的范围内,则不能在检查之前执行此操作。将它移到第一个 if
块内。
一般建议:
您可以使用调试器逐步从一条语句转到另一条语句,以找到(例如)抛出异常的确切源代码位置。
改变这个
tempInputDelInt = (int)(tempList[delSelection]);
至此
tempInputDelInt = (int)delSelection;
无论用户输入什么数字,您都在使用它作为索引来确定要删除的项目。所以这不是您从 tempList
获得的值。
感谢您的反馈。我仍然遇到超出范围的异常。我正在调试,但不明白为什么会出现异常。如果一个 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;
我在验证第一个 else 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);
我假设您的超出范围异常发生在这里:
tempInputDel = Convert.ToString(tempList[delSelection]);
如果 delSelection
在 tempList
的范围内,则不能在检查之前执行此操作。将它移到第一个 if
块内。
一般建议:
您可以使用调试器逐步从一条语句转到另一条语句,以找到(例如)抛出异常的确切源代码位置。
改变这个
tempInputDelInt = (int)(tempList[delSelection]);
至此
tempInputDelInt = (int)delSelection;
无论用户输入什么数字,您都在使用它作为索引来确定要删除的项目。所以这不是您从 tempList
获得的值。