如何在类中再次调用“static void Main(string[] args)”函数

3

我是一名新手,正在学习C#并且现在致力于控制台应用程序。以下是我写的代码:

Program.cs

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

namespace Ch06Ex01
{
    class Program
    {
        static void Write()
        {
            Console.WriteLine("Please enter any string..!!");
        }

         static void Main(string[] args)
        {       

            Write();
            string name = Console.ReadLine();
            Write();
            string name1 = Console.ReadLine();
            Write();
            string name2 = Console.ReadLine();
            Write();
            string name3 = Console.ReadLine();

             Console.WriteLine("{0}, {1}, {2}, {3}",name,name1,name2,name3);

             Console.WriteLine("enter \"y\" to restart the program and \"n\" to exit the Program");
             string selectedOption = Console.ReadLine();

             if (selectedOption == "y")
             {
                 // howto call  static void Main(string[] args) here agin so that the program start itself from the start point

             }    

             //else if (selectedOption == "n")
            {
               //Terminate the Program
             }
             Console.ReadKey();
         }      

    }

现在到了这个点:

 if (selectedOption == "y")
             {
                 // howto call  static void Main(string[] args) here agin so that the program start itself from the start point

             }    

我希望如果用户输入“y”,程序能够重新启动,如果用户输入“n”,程序能够终止。为了实现这个目的,我首先尝试使用goto语句,如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ch06Ex01
{
    class Program
    {
        static void Write()
        {
            Console.WriteLine("Please enter any string..!!");
        }

         static void Main(string[] args)
        {
            StartPoint;

            Write();
            string name = Console.ReadLine();
            Write();
            string name1 = Console.ReadLine();
            Write();
            string name2 = Console.ReadLine();
            Write();
            string name3 = Console.ReadLine();

             Console.WriteLine("{0}, {1}, {2}, {3}",name,name1,name2,name3);

             Console.WriteLine("enter \"y\" to restart the program and \"n\" to exit the Program");
             string selectedOption = Console.ReadLine();

             if (selectedOption == "y")
             {
                 // howto call  static void Main(string[] args) here agin so that the program start itself from the start point
                 goto StartPoint;
             }    

             //else if (selectedOption == "n")
             Console.ReadKey();
         }      

    }
}

但是在StartPoint;处,它对我无效,会出现错误。
Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement   C:\Users\Ahsan\Desktop\C# for Beginners Examples\Ch06Ex01\Ch06Ex01\Program.cs   18  13  Ch06Ex01

然后我尝试在该点调用主函数本身。
 if (selectedOption == "y")
         {
             // howto call  static void Main(string[] args) here agin so that the program start itself from the start point

         }    

但是这里给了我很多错误,现在不知道该怎么办,请问有谁能帮帮我吗?我不知道如何让这个东西工作起来... 更好的方法可能是在类中再次调用 " static void Main(string[] args) "。


1
你可以使用 while(Console.ReadKey()!='<some exit char>') {code here} - kravasb
2
静态void Main(string[] args)是C#控制台应用程序或Windows应用程序的入口点。当应用程序启动时,Main方法是首先被调用的方法。为什么你想要再次调用它呢? - Doro
2
当人们回答你所问的问题(如何调用你自己程序的根)时,我认为应该谨慎。除非线程遍历了Main的完整代码块,否则您的应用程序将不会结束。 在这种情况下,第一个对Main的调用将在内部main完成之前无法解决。 如果您不断递归,可能会冒堆栈溢出的风险。 但是,由于Main具有void返回类型,可能会发生一些尾调用优化。 使用“while”循环是解决此问题的典型C#方法。 - Adam Kewley
5个回答

10

首先,您的标签不正确。标签结尾应该有一个冒号字符:.. 因此,您的标签 应该是这样的:

StartPoint:

然而:

你应该循环直到满足某个条件。在这种情况下,条件是不重新启动:

bool running = true;

while (running) {
    /* 
     * all of your other code
     * should go here
     */
    if (selectedOption != "y") {
        running = false;
    }
}

5

您真的不应该使用“goto”或再次调用Main(递归),使用“do while”是重复多次逻辑的更好解决方案:

    string selectedOption;
    do {
        Write();
        string name = Console.ReadLine();
        Write();
        string name1 = Console.ReadLine();
        Write();
        string name2 = Console.ReadLine();
        Write();
        string name3 = Console.ReadLine();

         Console.WriteLine("{0}, {1}, {2}, {3}",name,name1,name2,name3);

         Console.WriteLine("enter \"y\" to restart the program and \"n\" to exit the Program");
         selectedOption = Console.ReadLine();
      } while (selectedOption == "y")
      Console.ReadKey();

1

只需再次调用该方法,传入最初传入的相同参数,同时尽可能避免使用goto语句

if (selectedOption == "y")
{
    // howto call  static void Main(string[] args) here agin so that the program start itself from the start point
    Main(args);
}    

3
您需要注意,如果递归层数足够多,这可能会导致堆栈溢出。这并不是正确处理您所面临情况的方式。我认为递归入口点并不是一件好事。 - Simon Whitehead
2
@OP 我同意,Simon的答案是更好的解决方案,这只是对你问题的一个快速回答。 - JMK

1
尝试将要执行的代码放在主类之外的另一个方法中,例如:
void execute()
{
        Write();
        string name = Console.ReadLine();
        Write();
        string name1 = Console.ReadLine();
        Write();
        string name2 = Console.ReadLine();
        Write();
        string name3 = Console.ReadLine();

         Console.WriteLine("{0}, {1}, {2}, {3}",name,name1,name2,name3);

         Console.WriteLine("enter \"y\" to restart the program and \"n\" to exit the Program");
         string selectedOption = Console.ReadLine();

}

然后在主函数中

static void Main(string[] args)
{
    bool run = true
    while(run){
       execute()
       Console.WriteLine("enter \"y\" to restart the program and \"n\" to exit the Program");
        selectedOption = Console.ReadLine();
        if (selectedOption == "n")
        {
             run = false;
        }    
    }
}

0

类似这样:

static void Main(string[] args)
            {      
                string selectedOption = "";

                 do 
                 {

                               ...........

                 }
                 while (selectedOption == "y")

                 if (selectedOption == "n")
                 {
                   //Terminate the Program
                  }

             } 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接