C#条件语句
我正在处理有条件的声明,并且很难在Windows Studio 2022中正确运行我的小型运输控制台。用户选择路线或选择不购买票证。
{
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");
}
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");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我希望您的代码看起来像这样 - 您检查响应值。
I'd expect your code to look something like this -- where you check the response value.
响应
的变量中的数据,并且永远不会使用它。NUM2
的变量NUM1
中设置数据,并使用 2 进行检查,您正在检查它们在条件下,在中。
您可以尝试创建函数以在IF-ELSE块中执行,以使您对代码的可读性更好。
response
and never using it.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.
首先,我在此页面上看到的答案是正确的。
看来您忘记了响应的使用。
但是我很难理解您的问题。
您将响应声明为VAR,然后决定使用INT。
没问题,如果愿意,可以做到。
但是,如果您想使用int,则需要将其放入try-catch块中,以免获得异常,以防用户输入字符串。
当您将VAR转换为INT时,此例外将会得到。
我添加了一些评论来解释代码。
如果您有疑问 - 请不要犹豫。
祝你好运 !
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 !