【发布时间】:2020-10-31 03:30:27
【问题描述】:
我大约需要一周的时间来学习 C#,之前的编码知识为 0。谁能告诉我为什么这不起作用?
string[] afirmatives = {"Yes", "yes", "YES", "Yeah", "yeah", "YEAH",
"Yep", "yep", "YEP", "Yup", "yup", "YUP", "Y", "y" };
/*string afirmatives = "Yes";*/
Console.Write("Are you a Human? ");
string humanAnswer = Console.ReadLine();
string answer = (humanAnswer = afirmatives) ?
"That sounds like something a robot would say."
: "Invalid Input";
Console.WriteLine(answer);
Console.ReadLine();
我知道我可以让它只使用一个字符串值,但是我需要做些什么不同的事情来使用多个?
我是否必须制作多个 if else 行?
谢谢!
编辑
Xareth 帮我找到了答案。我需要添加“包含”某些东西或其他东西。这是新的可操作代码。
string[] afirmatives = {"Yes", "yes", "YES", "Yeah", "yeah", "YEAH",
"Yep", "yep", "YEP", "Yup", "yup", "YUP", "Y", "y" };
Console.Write("Are you a Human? ");
string humanAnswer = Console.ReadLine();
string answer = humanAnswer = afirmatives.Contains(humanAnswer)
? "That's something a robot would say."
: "Invalid Input";
Console.WriteLine(answer);
Console.ReadLine();
【问题讨论】:
-
这能回答你的问题吗? Check if a value is in an array (C#)
-
欢迎来到 SO!虽然像 “我用 0 个先前的编码知识学习 C# 大约一周。” 可能适合也可能不适合在帖子中,但通常最好不要将其包含在帖子中 标题。标题应该是对问题的简短描述,其编写方式与使用搜索引擎时几乎相同(尽管在SO上它应该是一个独立子句)。这将帮助其他人在今天和将来帮助您。祝你好运
-
单个等号是赋值运算符而不是相等运算符。
标签: c#