如何通过用户响应设置Condition = true

时间:2018-03-21 16:12:19

标签: c# if-statement

Console.WriteLine("Unlike my deceased predecessor, A.I./Ai, I do not mind not being human. I am content with what I have.");
Console.ReadKey();
bool AI = false;
if (AI = true)            

我希望用户输入“AI”并将bool设置为true,如果他们这样做的话。

2 个答案:

答案 0 :(得分:2)

阅读完所有评论后,我认为这就是你的意思

Console.WriteLine("Unlike my deceased predecessor, A.I./Ai, I do not mind not being human. I am content with what I have.");

string answer = Console.ReadLine();

bool AI = (answer == "AI");

答案 1 :(得分:2)

您想使用Console.ReadLine,而不是Console.ReadKey。如果用户输入Aiai,您还应该使检查大小写不敏感。

Console.WriteLine("Unlike my deceased predecessor, A.I./Ai, I do not mind not being human. I am content with what I have.");
string lineRead = Console.ReadLine();
bool AI = "ai".Equals(lineRead, StringComparison.OrdinalIgnoreCase);
if(AI)
{
    // ai was selected
}