所以,我正在制作一个程序,它将为用户创建一个购物清单。它将启动,自我介绍,并提示您输入您的项目。当你输入商品后,它会将它们分类,如“乳制品”“干货”,这是我一直在努力解决的问题。
Console.WriteLine("-----------------------------------------------");
Console.WriteLine("[<I-I-I-I-I>] LOADING [<I-I-I-I-I>]");
System.Threading.Thread.Sleep(1000);
Console.WriteLine("Initiating GROCERMAN. . .");
Console.WriteLine("Welcome to GROCERMAN! GrocerMan will help you create the best grocery list ever.");
Console.WriteLine("Please enter the items you would like to purchase:");
string purchInp = Console.ReadLine();
Console.WriteLine($"Are you sure you want to buy these items: {purchInp}? Please enter 'Yes' or 'No' exactly or it will not register.");
var decbuy = Console.ReadLine();
if(decbuy == "Yes"){
Console.WriteLine("Awesome! Please wait one moment.");
Console.WriteLine("Alright, I made your list! Here it is:");
Console.WriteLine(purchInp);
}
else if(decbuy == "No"){
Console.WriteLine("Ah, very well. You can restart the program to make a new list or you could leave, I guess. . .");
}
Console.WriteLine("-----------------------------------------------");
我不知道如何获取purchInp
并阅读它,并自动将其分配给一个类别。我想,我可以将其与数组进行比较,这些数组包括一个项的尽可能多的可能性(即一个包含所有可能的乳制品的数组),以及如果它与某个数组中的一个项匹配(比如您输入"Milk“,并且与dairy数组中的"Milk”匹配,并将其打印在标题“Dairy”下)。然后就这样解决了,但我不知道怎么做。在理想情况下,它会像这样打印出来:
乳制品
牛奶
芝士
干货
谷类
面包
等等。。。
我也不知道如何打印这些换行符,我看到你可以做一个空的Console.WriteLine();
,但是当它打印出一整块数据时--通过打印出purchInp
,我看不出它是如何工作的。
发布于 2022-03-28 02:50:31
以下是一些您可以使用的工具:
用于可读性的List<string> items = purchInp.Split(",");
ToLower()
所以“是”和“是”都匹配用于拆分字符串的
foreach(var item in items) { Console.WriteLine(item) };
var knownItems = new Dictionary<string,List<string>>(); knownItems.Add("Diary", new List<string>() { "Milk", "Cream" });
https://stackoverflow.com/questions/71641745
复制相似问题