在字符串列表中查找ID

时间:2019-02-21 15:50:36

标签: c# list

我是编程方面的新手。我正在制定一种杂货店购物清单。我有一个字符串列表,用户可以在其中添加食品杂货:

List<string> list = new List<string>();

现在,如果用户要删除列表中的某个特定对象(杂货),我希望他使用数字来完成此操作,而不是在控制台窗口中输入杂货店名称,这是我在这里完成的:

case 2:
Console.WriteLine("What would you like to remove from the list?");
int removeGroc = Convert.ToInt32(Console.ReadLine());
list.RemoveAt(removeGroc);
break;

我想做的是确保检查用户是否输入了数字5,但列表中只有3个对象,因此,由于其中只有3个对象,因此系统会要求用户再次输入数字。列表(0、1和2)。我到处搜索过并尝试了不同的方法,但是它们似乎没有用,或者我的知识很难理解。那我该怎么做呢?我对我需要做的事情很了解,但我不知道该怎么做。我在想我需要找到列表的“ id”,然后检查它是否存在,但是我似乎找不到在线操作的方法。

如果有任何其他新手看到此评论,这里的编辑就可以通过评论来工作了

Console.WriteLine("What would you like to remove from the list?");
var removeGroc = Console.ReadLine();
int removeGrocId;
bool parseSuccess = int.TryParse(removeGroc, out removeGrocId);
if (removeGrocId < list.Count)
   list.RemoveAt(removeGrocId);
else
   Console.WriteLine("Write a valid number!");

2 个答案:

答案 0 :(得分:2)

您可以使用

list.Count; //return the number of the items in the list

因此,您可以使用列表中的项目数进行验证:

if(removeGroc > listCount){ //do someting }

答案 1 :(得分:2)

我发现使用帮助程序方法从用户那里获取电话号码真的很有用。此方法使用TryParse将输入转换为数字,这非常方便,因为它返回指示成功的bool,然后将转换后的数字设置为out参数。请注意,在上述方法中,如果用户输入的数字无效,例如“两个”,则会引发异常。

下面的方法还接受一个可选的字符串“ prompt”,该字符串将显示给用户,然后循环播放直到输入有效值为止。

private static int GetIntFromUser(string prompt)
{
    int input;

    do
    {
        Console.Write(prompt);
    } while (!int.TryParse(Console.ReadLine(), out input));

    return input;
}

现在,要从用户那里获取一个整数,我们可以这样做:

int removeGroc = GetIntFromUser("What would you like to remove from the list?");

可以通过添加一项功能来稍微改善此功能,我们可以使用Console.CursorTop,而不是在新行中再次提问(如果控制台行继续输入错误的数据,它将慢慢占用控制台窗口空间)。和Console.SetCursorPosition用空格覆盖当前行(清除其原始输入),然后在同一行上再次写问题:

private static int GetIntFromUser(string prompt)
{
    int result;
    var cursorTop = Console.CursorTop;

    do
    {
        // Set the cursor to the beginning of the line,
        // write a blank line, and set it to the beginning again
        Console.SetCursorPosition(0, cursorTop);
        Console.Write(new string(' ', Console.WindowWidth));
        Console.SetCursorPosition(0, cursorTop);
        Console.Write(prompt);
    } while (!int.TryParse(Console.ReadLine(), out result));

    return result;
}

好的,现在我们只需要添加一种方法来指定有效的条目,这样,如果它们输入了一个数字,但对我们的场景无效,它将继续询问他们有效的输入。

我做的第一件事是写了很多这种方法的重载,它们吸收了minValuemaxValue,甚至是List<int> validNumbersList<int> invalidNumbers之类的东西,然后有逻辑检查输入的数字是否满足所有这些条件。

但是,由于我们有能力将函数传递给方法,因此我选择让客户端传递自己的验证函数,该验证函数接受{ {1}}并返回一个int。这样,相同的方法可以在各种情况下重复使用:

bool

现在,我们有了一种方法,该方法可以进行所需的任何类型的验证,并将其应用于用户输入。对于您的示例,要求该数字大于或等于private static int GetIntFromUser(string prompt, Func<int, bool> validator = null) { int result; var cursorTop = Console.CursorTop; do { Console.SetCursorPosition(0, cursorTop); Console.Write(new string(' ', Console.WindowWidth)); Console.SetCursorPosition(0, cursorTop); Console.Write(prompt); } while (!int.TryParse(Console.ReadLine(), out result) || (validator != null && !validator.Invoke(result))); return result; } ,并且小于或等于0(以在list.Count - 1中获得有效索引)。可以将其写为lambda方法,例如:

list

因此,将其应用于您的示例,我们现在可以简单地执行以下操作:

i => i >= 0 && i <= list.Count - 1