C# 使用通配符或;标签;供用户输入
我最近开始学习C#,之前没有编程经验。我一直在学习教程,我正在学习“如果”语句。我正在尝试创建一个简单的用户反馈应用程序,它询问问题并响应用户输入 我想做的是使用某种关键字或标记系统或通配符类型系统(如搜索查询中的*)来允许对不完全特定的输入进行响应 例如,在下面的代码中,我使用If和Else语句,是否有方法将userValue设置为不仅等于“good”或“bad”,而且等于这两个词上的任意数量的变体(例如,“I'm very good”),或者让If语句引用关键字或标记列表(可能在别处列出,或者在外部文件中列出)这样,用户就可以随心所欲地输入,而预先确定的程序就会记住这个短语 我不是想创造某种形式的人工智能,只是想制作一个有趣的响应程序。是否可以/合理地使用数组,并以某种方式将其作为If语句调用?还是有其他更有效的方法向用户输入提供反馈?我很抱歉,如果这对这个网站来说太新手了。我在网上搜索过,但问题是,我不知道我在搜索什么 我目前掌握的守则如下:C# 使用通配符或;标签;供用户输入,c#,if-statement,wildcard,C#,If Statement,Wildcard,我最近开始学习C#,之前没有编程经验。我一直在学习教程,我正在学习“如果”语句。我正在尝试创建一个简单的用户反馈应用程序,它询问问题并响应用户输入 我想做的是使用某种关键字或标记系统或通配符类型系统(如搜索查询中的*)来允许对不完全特定的输入进行响应 例如,在下面的代码中,我使用If和Else语句,是否有方法将userValue设置为不仅等于“good”或“bad”,而且等于这两个词上的任意数量的变体(例如,“I'm very good”),或者让If语句引用关键字或标记列表(可能在别处列出,或
Console.WriteLine("Hello. How are you?");
string userValue = Console.ReadLine();
string message = "";
if (userValue == "good")
message = "That's good to hear. Do you want a cup of tea?";
else if (userValue == "bad")
message = "I'm sorry to hear that. Shall I make you a coffee?";
else
message = "I don't understand. Do you want a cuppa?";
Console.WriteLine(message);
string userValueTwo = Console.ReadLine();
string messageTwo = "";
if (userValueTwo == "yes")
messageTwo = "I'll get right onto it!";
else if (userValueTwo == "no")
messageTwo = "Right-o. Shutting down...";
Console.WriteLine(messageTwo);
Console.ReadLine();
一张简单的支票怎么样
我还添加了case转换,这样无论是哪种情况,它都可以工作
如果您想要实现一个关键字和程序(函数)列表,我会这样做:
var keywords = new Dictionary<string, System.Func<string>>() {
{ "good", () => "That's good to hear. Do you want a cup of tea?" },
{ "bad", () => "I'm sorry to hear that. Shall I make you a coffee?" }
};
foreach(var keyword in keywords)
{
if (userValue.ToLower().Contains(keyword.Key))
{
message = keyword.Value();
break;
}
}
var关键字=新字典(){
{“很好”,“很高兴听到你这么说。你想喝杯茶吗?”},
{“糟糕”,“听到这个消息我很难过。我给你煮杯咖啡好吗?”}
};
foreach(关键字中的var关键字)
{
if(userValue.ToLower().Contains(keyword.Key))
{
message=关键字.Value();
打破
}
}
在本例中,我使用保存您想要的“程序列表”;这是一个非常强大的C#特性,允许将代码用作数据。现在,这些函数只返回常量字符串值,因此对于您的场景来说,它们有点过头了。尝试使用“Contains”方法
例如:
if (userValue.ToLower().Contains("good"))
message = "That's good to hear. Do you want a cup of tea?";
您可以在此处使用正则表达式:
我犹豫是否发布这个答案,因为它使用LINQ,这可能会让你感到困惑,因为你只是在学习。但它比可怕的正则表达式更简单!您可以使用自己的循环来实现这一点,但LINQ只是为您保存了一些代码,并使其(可以说)更具可读性:
Console.WriteLine("Hello. How are you?");
string userValue = Console.ReadLine();
string message = "";
string[] goodWords = new string[] { "good", "well", "sweet", "brilliant"};
string[] badWords = new string[] { "terrible", "awful", "bad", "sucks");
if (goodWords.Any(word => userValue.Contains(word)))
message = "That's good to hear. Do you want a cup of tea?";
else if (badWords.Any(word => userValue.Contains(word)))
message = "I'm sorry to hear that. Shall I make you a coffee?";
else
message = "I don't understand. Do you want a cuppa?";
基本上,Any()
函数会查看列表中是否有符合某些条件的单词。我们使用的标准是userValue
字符串是否包含该单词()。
看起来很有趣的=>语法是一种非常简单的方法,只是一种编写匿名函数的快速方法。再说一次,有些事情现在可能有点太让人困惑了
以下是一个非LINQ版本,您可能会发现它更容易理解:
void main()
{
Console.WriteLine("Hello. How are you?");
string userValue = Console.ReadLine();
string message = "";
string[] goodWords = new string[] { "good", "well", "sweet", "brilliant"};
string[] badWords = new string[] { "terrible", "awful", "bad", "sucks"};
if (DoesStringContainAnyOf(userValue, goodWords))
message = "That's good to hear. Do you want a cup of tea?";
else if (DoesStringContainAnyOf(userValue, badWords))
message = "I'm sorry to hear that. Shall I make you a coffee?";
else
message = "I don't understand. Do you want a cuppa?";
string answer = "I'm really well thanks";
}
bool DoesStringContainAnyOf(string searchIn, string[] wordsToFind)
{
foreach(string word in wordsToFind)
if (searchIn.Contains(word))
return true;
return false;
}
之前所有的答案都是有效的,包含了许多值得学习的东西。我想特别注意lower
方法,它将帮助您识别“Good”和“Good”
此外,为了使列表更加完整,您应该了解好的旧IndexOf
方法,该方法将返回字符串中子字符串的位置,如果不包含子字符串,则返回-1
但我有一种预感,你的问题也针对如何以更有效的方式编写Eliza代码(当然是游戏名称)的问题。你肯定想问两个以上的问题
如果提示词和回答可以很容易地扩展,而无需再次更改和编译程序,这是最有趣的
最简单的方法是将所有数据放在一个文本文件中;有很多方法可以做到这一点,但最简单的维护方法是采用以下逐行格式:
//The text file eliza.txt:
good=That's good to hear. Do you want a cup of tea?
bad=I'm sorry to hear that. Shall I make you a coffee?
father=Tell me more about your family!
mother=What would you do Daddy?
..
bye=Goodbye. See you soon..
这是通过File.ReadLines
命令读入的。使用System.IO添加如果程序尚不存在,请将“代码>添加到程序顶部…”。。
从这里开始,我建议使用字典
Dictionary-eliza=newdictionary();
var lines=File.ReadAllLines(“D:\\eliza.txt”);
foreach(行中的字符串行)
{
var parts=line.Split('=');
如果(parts.Length==2)eliza.Add(parts[0].ToLower(),parts[1]);
}
现在,您可以创建一个循环,直到用户说“再见”或什么都不说:
string userValue = Console.ReadLine();
do
{
foreach (string cue in eliza.Keys)
if (userValue.IndexOf(cue) >= 0)
{ Console.WriteLine(eliza[cue]); break; }
userValue = Console.ReadLine().ToLower();
}
while (userValue != "" && userValue.IndexOf("bye") < 0);
string userValue=Console.ReadLine();
做
{
foreach(eliza.Keys中的字符串提示)
if(userValue.IndexOf(cue)>=0)
{Console.WriteLine(eliza[cue]);break;}
userValue=Console.ReadLine().ToLower();
}
while(userValue!=”&&userValue.IndexOf(“bye”)<0);
要扩展的有趣内容包括提示词列表、响应列表、使用后删除一些响应或添加一些随机响应。请注意,正则表达式甚至会让经验丰富的开发人员喝酒!有人能告诉我为什么Daddy
会突出显示语法吗???)
//The text file eliza.txt:
good=That's good to hear. Do you want a cup of tea?
bad=I'm sorry to hear that. Shall I make you a coffee?
father=Tell me more about your family!
mother=What would you do Daddy?
..
bye=Goodbye. See you soon..
Dictionary<string, string> eliza = new Dictionary<string, string>();
var lines = File.ReadAllLines("D:\\eliza.txt");
foreach (string line in lines)
{
var parts = line.Split('=');
if (parts.Length==2) eliza.Add(parts[0].ToLower(), parts[1]);
}
string userValue = Console.ReadLine();
do
{
foreach (string cue in eliza.Keys)
if (userValue.IndexOf(cue) >= 0)
{ Console.WriteLine(eliza[cue]); break; }
userValue = Console.ReadLine().ToLower();
}
while (userValue != "" && userValue.IndexOf("bye") < 0);