C# 如何使我的控制台应用程序在成功运行后自动重新启动?

C# 如何使我的控制台应用程序在成功运行后自动重新启动?,c#,function,console-application,calculator,C#,Function,Console Application,Calculator,在用户选择一个选项后,如何使该程序继续运行?这是我到目前为止的代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace calculator_extended { class Program { static void Main(string[] args) { Console.Wri

在用户选择一个选项后,如何使该程序继续运行?这是我到目前为止的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace calculator_extended
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Press A for addition");
            Console.WriteLine("Press S for subtraction");
            Console.WriteLine("Press M for Multiplication");
            Console.WriteLine("Press D for Divide");

            char c = Convert.ToChar(Console.ReadLine());

            int a = Convert.ToInt32(Console.ReadLine());
            int b = Convert.ToInt32(Console.ReadLine());

            switch (c)
            {
                case 'A':
                case 'a':
                    {
                        int d = add(a, b);
                        Console.WriteLine(d);
                        break;


                    }


                case 'S':
                case 's':
                    {
                        int d = sub(a, b);
                        Console.WriteLine(d);
                        break;
                    }

                case 'M':
                case 'm':
                    {
                        int d = mul(a, b);
                        Console.WriteLine(d);
                        break;
                    }

                case 'E':
                case 'e':
                    {
                        int d = div(a, b);
                        Console.WriteLine(d);
                        break;
                    }
                default:
                    {

                        Console.WriteLine("Please Enter the correct Character");
                        break;
                    }


            }
        }
            private static int add(int a, int b)
    {

                   return a + b;
    }
               private static int sub(int a, int b)
    {

                   return a - b;
    }
               private static int mul(int a, int b)
    {
                   return a * b;
    }
               private static int div(int a, int b)
    {

                   return a / b;
    }

        }
    }

while(true) {
    // Your code here

    // Don't forget to add a switch case for an Exit operation
    case 'q':
        Application.Exit();
}
如果您是编程新手,您应该研究一下——请参见清单4-2——这是一个很好的示例,说明了您正在努力实现的目标

编辑: 我意识到你是编程新手,我认为你应该自己完成这项工作。 因为这是一个基本问题,这里有一个完整的解决方案

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace calculator_extended
{
    class Program
    {
        static void Main(string[] args)
        {
            int d = 0;

            while (true)
            {
                Console.WriteLine("Press A for addition");
                Console.WriteLine("Press S for subtraction");
                Console.WriteLine("Press M for Multiplication");
                Console.WriteLine("Press D for Divide");

                char c = Convert.ToChar(Console.ReadLine());

                int a = Convert.ToInt32(Console.ReadLine());
                int b = Convert.ToInt32(Console.ReadLine());

                switch (c)
                {
                    case 'A':
                    case 'a':
                        d = add(a, b);
                        Console.WriteLine(d);
                        break;
                    case 'S':
                    case 's':
                        d = sub(a, b);
                        Console.WriteLine(d);
                        break;
                    case 'M':
                    case 'm':
                        d = mul(a, b);
                        Console.WriteLine(d);
                        break;
                    case 'E':
                    case 'e':
                        d = div(a, b);
                        Console.WriteLine(d);
                        break;
                    case 'q':
                    case 'Q':
                        Application.Exit();
                    default:
                        Console.WriteLine("Please Enter the correct Character");
                        break;
                }
            }
        }
        private static int add(int a, int b)
        {

            return a + b;
        }
        private static int sub(int a, int b)
        {

            return a - b;
        }
        private static int mul(int a, int b)
        {
            return a * b;
        }
        private static int div(int a, int b)
        {

            return a / b;
        }

    }
}

这可以通过循环来解决。对于这种控制台应用程序,我通常使用Do…While循环


在学习的过程中你会遇到这些事情。。。我建议您在尝试编写您还没有完成的代码之前,继续学习您正在学习的任何引导式学习课程。

您需要了解,这就是您在这里使用的方法,以使程序保持循环,直到满足某些条件,即选择计算,然后输入第一个值,然后输入第二个值。比如:

static void Main(string[] args)
{
    // main loop to keep your program alive
    while (true)
    {
        // handle your program logic
    }
}

将代码放入
do块

        char c = '';
        do
        {
            c = ...
        }
        while (c != 'q');

您可以使用goto命令进行迭代的任何一种技术,通过不满意while或do while循环调用函数,通过infinte for loop

首先,为了使程序继续,您必须将其包装为while(true)循环,或do{}while(按下某个键)循环。您必须在选项列表中添加第五个选项^^(这将是退出选项)

我相信要解决你的问题,你有两个选择:

  • 使用while(true)循环

    main()方法中的所有代码都将包装在while(true)循环中。完成此操作后,您将读取退出键,就像读取任何其他键(A、S、M、D)一样,并将其发送到正确的退出方法。此“退出方法”将在主函数之外创建。完成后,它应该如下所示:

    static void Main(string args[])
    {
        Console.WriteLine("Press A for addition");
        Console.WriteLine("Press S for subtraction");
        Console.WriteLine("Press M for Multiplication");
        Console.WriteLine("Press D for Divide");
        Console.WriteLine("Press X to exit");
        //... all the stuff up to the switch
        switch(c)
        {
            //all the cases from before
            case 'x' : exitMethod();break;
        }
    }
    private static void exitMethod()
    {
        Application.Exit();
    }
    
  • 使用do-while循环

  • main()方法中的所有代码都将包装在do-while循环中。是的,这类似于使用while(true)循环,但是这次您必须更改while中的语句。它将是这样的:

        do { // here is all the code in main() 
        }while(Console.ReadKey(true).Key != ConsoleKey.X);
    
    这就是do-while循环。您不需要调用特殊的exit方法,因为程序将跳出do while循环,并像现在一样(通常)关闭自己

    老实说,我更愿意使用do-while循环,因为它还可以处理多个键的组合。
    有关更多信息,请访问此链接:

    欢迎访问SO!确保你为你的问题选择了一个合适的标题。当前的问题更像是一种陈述,根本无法解释您的问题。我编辑了您的问题标题,请查看这是否正确反映了您的问题。我以后会小心jakub谢谢:)将非常感谢上面提供的代码的完整示例:)非常感谢我以完全相同的方式执行此操作,但我没有获得应用程序。exit();intellisense正在使用的函数我正在使用visual studio 2010。您是否导入了
    system.windows.forms
    ?我正在使用控制台应用程序。我是否必须使用system.windows.forms库?不,只需使用
    system.windows.forms.application.Exit()并不重要