For example
Console.WriteLine("Please input your age: ");
string age = Console.ReadLine();
Console.WriteLine("Please input your first name: ");
string firstname = Console.ReadLine();
Console.WriteLine("Please input your last name: ");
string lastname = Console.ReadLine();
Console.WriteLine("Thank you!");
Now pretend I have something like this but with a lot of more questions. Then I wouldn't want to keep putting an if
statement on each one. How would I make it run this code:
Console.WriteLine("beta")
whenever the user says "alpha" without putting if
statements on each user input?
As already suggested, make a function that receives a prompt and returns the response. Inside that, you can check for "alpha" and output "beta", though I suspect that's not ~really~ what you need to do:
static void Main(string[] args)
{
string age = GetResponse("Please input your age: ");
string firstname = GetResponse("Please input your first name: ");
string lastname = GetResponse("Please input your last name: ");
Console.WriteLine("Thank you!");
Console.Write("Press Enter to Quit");
Console.ReadLine();
}
static string GetResponse(string prompt)
{
Console.Write(prompt);
string response = Console.ReadLine();
if (response == "alpha")
{
Console.WriteLine("beta");
}
return response;
}