I making a text adventure game and I'm stuck with making a y/n option.
This is my code. BTW I'm to new to coding like one night new.
Console.WriteLine("Are You Ready For The Day My Lord [y/n]");
Console.ReadLine();
Sorry if this too easy.
CodePudding user response:
You can go with someting like this
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
CodePudding user response:
Something like this can be your case
ConsoleKeyInfo k = Console.ReadKey();
if (k.Key.ToString() == "y")
{
//do what you need for yes
}
else
{
// presses something other then Y
}
CodePudding user response:
I would suggest to use string.Equals to compare strings so something like this should work properly:
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");
}
This if you want only "y" or "n"