我做了一个文字冒险游戏,我坚持做一个y/n选项。
这是我的密码。顺便说一句,我对编码很陌生,就像一夜新。
Console.WriteLine("Are You Ready For The Day My Lord [y/n]");
Console.ReadLine();抱歉,如果这太简单了。
发布于 2022-07-26 10:34:28
你可以做这样的事
Console.WriteLine("Are You Ready For The Day My Lord [y/n]");
string yesNo = Console.ReadLine(); //get the answer
if(yesNo == "y") //check the answer
Console.WriteLine("You are ready."); //write something for option y
else
Console.WriteLine("You are NOT ready."); //write something for other option发布于 2022-07-26 10:53:39
我建议使用string.Equals来比较字符串,这样的东西应该能正常工作:
Console.WriteLine("Are You Ready For The Day My Lord [y/n]");
string userInput = Console.ReadLine();
if(string.Equals(userInput, "y"))
{
Console.WriteLine("You answered yes");
}
else
{
Console.WriteLine("You answered no");
}如果你只想要"y“或"n”的话
发布于 2022-07-27 08:28:44
听起来你要做很多这样的事情,所以也许把这类事情封装在一个助手类中。
public static class Prompt
{
public bool GetYesNo(string input)
{
Console.Writeline(input + " [y/n]");
var result = Console.ReadLine().ToLower();
if(result == "y") return true;
if(result == "n") return false;
Console.WriteLine("Invalid input");
return GetYesNo(input);
}
}然后在控制台应用程序中:
var ready = Prompt.GetYesNo("Are You Ready For The Day My Lord");
if(ready)
{
// do something
}https://stackoverflow.com/questions/73121749
复制相似问题