C#条件语句

发布于 2025-02-13 11:52:32 字数 695 浏览 1 评论 0原文

我正在处理有条件的声明,并且很难在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

A君 2025-02-20 11:52:32

我希望您的代码看起来像这样 - 您检查响应值。

 {
   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");
     }
  } 

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");
     }
  } 
咆哮 2025-02-20 11:52:32
  1. 您正在读取称为响应的变量中的数据,并且永远不会使用它。
  2. 您正在使用Value 1 NUM2的变量NUM1中设置数据,并使用 2 进行检查,您正在检查它们在条件下,在中。

您可以尝试创建函数以在IF-ELSE块中执行,以使您对代码的可读性更好。

  1. You are reading the data in the variable called response and never using it.
  2. You are setting the data in the variable num1 with value 1 and num2 with 2, and you are checking them in the if condition.

You can try to create functions to execute within the if-else blocks to give you better readability over the code.

韶华倾负 2025-02-20 11:52:32

首先,我在此页面上看到的答案是正确的。
看来您忘记了响应的使用。
但是我很难理解您的问题。
您将响应声明为VAR,然后决定使用INT。
没问题,如果愿意,可以做到。
但是,如果您想使用int,则需要将其放入try-catch块中,以免获得异常,以防用户输入字符串。
当您将VAR转换为INT时,此例外将会得到。
我添加了一些评论来解释代码。
如果您有疑问 - 请不要犹豫。
祝你好运 !

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");
        }
    }

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");
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文