I'm working on a conditional statement and I'm having difficulty getting my small transportation console application to run properly in Windows Studio 2022. After pressing 1 or 2 (Yes or No) my application goes back to my main menu instead of proceeding to the user choosing a route or choosing not to purchase a ticket.
{
Console.WriteLine("Would you like to buy a Ticket?\n");
Console.WriteLine("Please Type 1 for: Yes");
Console.WriteLine("Please Type 2 for: No");
var response = Console.ReadLine();
Console.WriteLine(response);
int num = 1;
if (num == 1)
{
Console.WriteLine("1) For the first route option, please type 1");
Console.WriteLine("2) For the second route option, please type 2");
}
int num2 = 2;
if (num2 == 2)
{
Console.WriteLine("No Ticket Purchased: Have a great day!");
}
else
{
Console.WriteLine("Your answer was not vaild");
}
CodePudding user response:
I'd expect your code to look something like this -- where you check the response value.
{
Console.WriteLine("Would you like to buy a Ticket?\n");
Console.WriteLine("Please Type 1 for: Yes");
Console.WriteLine("Please Type 2 for: No");
var response = Console.ReadLine();
Console.WriteLine(response);
if (response == '1')
{
// do something to let them buy a ticket
}
else
if (response == '2')
{
Console.WriteLine("No Ticket Purchased: Have a great day!");
}
else
{
Console.WriteLine("Your answer was not vaild");
}
}
CodePudding user response:
- You are reading the data in the variable called
response
and never using it. - You are setting the data in the variable
num1
with value 1 andnum2
with 2, and you are checking them in theif
condition.
You can try to create functions to execute within the if-else blocks to give you better readability over the code.
CodePudding user response:
First , the answers that I see in this page is correct. it seems that you forgot the using of the response. But I’m having trouble understanding your question. You declare response as a var and then you decide to use int. No problem, you can make it if you want. But if you want to use int , you need to put it in try-catch block in order to avoid get an exception in case user will enter string. This exception will get when you convert the var to int. I add some comments to explain the code. if you have questions - don't hesitate to ask. goodluck !
private static void BuyTicket(bool retry = true)
{
int selection = 0;
Console.WriteLine("Would you like to buy a Ticket?\n");
Console.WriteLine("Please Type 1 for: Yes");
Console.WriteLine("Please Type 2 for: No");
var response = Console.ReadLine();
//for case user enter a string and not a number, so need to give friendly message of the options available
try
{
selection = int.Parse(response);
}
catch (Exception ex)
{
Console.WriteLine("Please choose '1' or '2' only");
Console.WriteLine(ex.Message);
if (retry == true)
{
//if the user will enter invalid data this time , it will skip and continue
BuyTicket(false);
}
}
if (selection == 1)
{
Console.WriteLine("You choosed to buy a ticket.");
Console.WriteLine("1) For the first route option, please type 1");
Console.WriteLine("2) For the second route option, please type 2");
response = Console.ReadLine();
Console.WriteLine("Thanks for your choice. You choose " int.Parse(response) " route option");
}
else if (selection == 2)
{
Console.WriteLine("No Ticket Purchased: Have a great day!");
}
else
{
Console.WriteLine("Your answer was not vaild. Please enter '1' or '2' only");
}
}