【问题标题】:how to call " static void Main(string[] args) "in the class again如何在课堂上再次调用“static void Main(string[] args)”
【发布时间】:2013-11-08 11:10:18
【问题描述】:

我是 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

然后我尝试调用 main 函数本身

 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) ”将是首选....

【问题讨论】:

  • 你可以使用while(Console.ReadKey()!='<some exit char>') {code here}
  • Static void Main(string[] args) 是 C# 控制台应用程序或 Windows 应用程序的入口点。启动应用程序时,Main 方法是调用的第一个方法。为什么又要召回?
  • 当人们回答你的问题时(如何调用你自己的程序根),我会小心这样做。在线程遍历Main 的完整代码块之前,您的应用程序不会结束。在这种情况下,对 Main 的第一次调用将在内部 main 完成之前解析。如果你像这样不断地递归,你可能会冒堆栈溢出的风险。但是,Main 具有 void 返回可能会导致发生一些尾调用优化。 while 循环是解决此问题的惯用 C# 方法。

标签: c# .net console console-application


【解决方案1】:

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

StartPoint:

但是

您应该循环直到满足条件。在这种情况下..条件是不重启:

bool running = true;

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

【讨论】:

    【解决方案2】:

    您真的不应该使用 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();
    

    【讨论】:

      【解决方案3】:

      只需再次调用该方法,传入与最初传入相同的参数,同样是try to avoid using goto if you can

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

      【讨论】:

      • @OP:您应该知道,这可能会导致堆栈溢出......如果有足够的递归。这并不是处理您所遇到情况的正确方法。我无法想象递归入口点是一件好事。
      • @OP 我同意,Simons 的回答是更好的解决方案,这只是对您问题的快速回答。
      【解决方案4】:

      尝试将您将在主类之外执行的代码放在另一个方法中,例如

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

      【讨论】:

        【解决方案5】:

        类似这样的:

        static void Main(string[] args)
                    {      
                        string selectedOption = "";
        
                         do 
                         {
        
                                       ...........
        
                         }
                         while (selectedOption == "y")
        
                         if (selectedOption == "n")
                         {
                           //Terminate the Program
                          }
        
                     } 
        

        【讨论】:

          猜你喜欢
          • 2016-05-22
          • 2013-06-03
          • 2012-08-10
          • 2018-11-12
          • 1970-01-01
          • 2020-08-25
          • 1970-01-01
          • 1970-01-01
          • 2020-04-05
          相关资源
          最近更新 更多