gpt4 book ai didi

c# - 信息重新 : add/remove methods in stringLists

转载 作者:行者123 更新时间:2023-11-30 22:30:12 29 4
gpt4 key购买 nike

当前代码目前编译正常,如果发现重复项,它会自动删除。无论如何,用户可以选择是否要在删除之前保留副本。或者我可以看看是否有其他合适的方法。

 static void Main(string[] args)
{
List<string> dictionaryList = new List<string>();
string input;

Console.Write("Please enter a string or END to finish: ");
input = Console.ReadLine();
while (input.ToUpper() != "END")
{
if (!dictionaryList.Contains(input)) // This is where i am looking of a user response of y/n too add duplicate string

dictionaryList.Add(input);

Console.Write("Please enter a string or END to finish: ");
input = Console.ReadLine();

}

dictionaryList.Sort();
Console.WriteLine("Dictionary Contents:");
foreach (string wordList in dictionaryList)
Console.WriteLine("\t" + wordList);
}
}
}

最佳答案

这应该可以解决问题:

while (input.ToUpper() != "END")
{
bool blnAdd = true;
if (dictionaryList.Contains(input))
{
Console.Write("Already exists, keep the duplicate? (Y/N)");
blnAdd = Console.ReadLine().Equals("Y");
}

if (blnAdd)
dictionaryList.Add(input);

Console.Write("Please enter a string or END to finish: ");
input = Console.ReadLine();
}

代码背后的逻辑:如果输入已存在于列表中,则提醒用户并阅读他的答案 - 仅当 Y 添加该项目时。

关于c# - 信息重新 : add/remove methods in stringLists,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9803029/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com