如果选择退出选项,如何退出程序?

时间:2020-10-19 00:58:18

标签: c#

晚上好。

对于菜鸟问题​​的道歉,我是C#的新手,所以我仍然在处理一些事情。我正在尝试编写一个如下所示的简单程序,但是我正在努力为退出功能获取正确的编码。如果输入了退出选项,因为“如果choice == 4”似乎不起作用,如何编写此代码以退出程序?

// Demonstrate branching and loops.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Fundamentals
{
    class Program
    {
        static void Main(string[] args)
        {

            do
            {

                // Example: Switch.
                Console.WriteLine("Main Menu");
                Console.WriteLine("1 - Play. ");
                Console.WriteLine("2 - Options. ");
                Console.WriteLine("3 - Help.  ");
                Console.WriteLine("4 - Exit.  ");
                Console.WriteLine(" ");
                int choice = Convert.ToInt32(Console.ReadLine());
                switch (choice)
                {
                    case 1:
                        Console.WriteLine("Playing the game.");
                        Console.WriteLine("");
                        // Example: do... while loop.
                        double ans;
                        do
                        {
                            Console.Write("What is: 10 x 10 = ");
                            ans = Convert.ToInt32(Console.ReadLine());
                        } while (ans != 100);
                        Console.WriteLine("\nCorrect!\n");
                        break;
                    case 2:
                        Console.WriteLine("Game settings.\n");
                        break;
                    case 3:
                        Console.WriteLine("Game Help.\n");
                        break;
                    case 4:
                        Console.WriteLine("Exiting...\n");
                        break;
                    default:
                        break;
                }
             
                if (choice == "4")
                {
                    Environment.Exit(0);
                }

            } while (true);

        }
    }
}

2 个答案:

答案 0 :(得分:2)

选择是int,您正在将其与string进行比较-所以

if (choice == 4)
{
    Environment.Exit(0);
}

对于您发布的代码-但您可以将退出代码放在这种情况下:

case 4: 
{
    Console.WriteLine("Exiting...\n");
    Environment.Exit(0);
    break;
}

答案 1 :(得分:1)

通过将Environment.Exit()调用放入正确比较两个int的switch语句内,而不是在评估条件之后重新评估条件,您就可以避免出现此问题。

那些说int永远不等于string的东西是钱,但是如果我可能提出一个更正确的解决方案,那么您根本就不需要if语句。

 switch (choice)
            {
                case 1:
                    Console.WriteLine("Playing the game.");
                    Console.WriteLine("");
                    // Example: do... while loop.
                    double ans;
                    do
                    {
                        Console.Write("What is: 10 x 10 = ");
                        ans = Convert.ToInt32(Console.ReadLine());
                    } while (ans != 100);
                    Console.WriteLine("\nCorrect!\n");
                    break;
                case 2:
                    Console.WriteLine("Game settings.\n");
                    break;
                case 3:
                    Console.WriteLine("Game Help.\n");
                    break;
                case 4:
                    Console.WriteLine("Exiting...\n");
                    Environment.Exit(0);  //// Just exit from here...
                default:
                    break;
            }