2013-10-29 20 views
0

我在C#中新的控制檯應用程序的工作現在是一個天我寫了下面的代碼:如何調用「靜態無效的主要(字串[] args)」中的類再次

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 

     }  

,但在這裏給了我很多的錯誤,不要懂得d現在呢,任何人都可以幫助我嗎?不知道如何做這個事情.... 調用「靜態無效主(字符串[] ARGS)」在該班再次將首選....

+1

你可以用'而(Console.ReadKey() !=''){code here}' – kravasb

+1

Static void Main(string [] args)是C#控制檯應用程序或Windows應用程序的入口點。當應用程序啓動時,Main方法是第一個被調用的方法。你爲什麼想再回想一遍? – Doro

+2

雖然人們給你一個答案(如何調用你自己的程序根目錄),但我會小心這樣做。直到線程遍歷Main的完整代碼塊,您的應用程序纔會結束。在這種情況下,直到內部'main'完成後,第一次調用'Main'纔會被解析。如果你不斷地這樣做,你可能會冒着堆棧溢出的風險。但是,具有「空」返回值的'Main'可能會導致一些尾部呼叫優化的發生。 while循環是這個問題的慣用C#方法。 –

回答

1

再打電話的方法,通過在相同的論點,凡在最初過去了,也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); 
}  
+3

@OP:你應該知道,這可能會導致一個stackoverflow ..給予足夠的遞歸。這不是處理你所遇到的情況的正確方法。我無法想象遞歸入口點是件好事。 –

+2

@OP我同意,西蒙斯的回答是一個更好的解決方案,這只是對您的問題的快速回答。 – JMK

9

首先,你的標籤是不正確的。標籤的結束應該有一個冒號: ..所以你的標籤應該一直這樣:

StartPoint: 

然而

你應該只循環,直到滿足條件。在這種情況下..條件是要不要重啓:

bool running = true; 

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

你真的不應該使用goto語句或致電主再次(遞歸),而做一個更好的解決方案來重複你的邏輯muliple時間:

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(); 
0

是這樣的:

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

       do 
       { 

           ........... 

       } 
       while (selectedOption == "y") 

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

      } 
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; 
     }  
    } 
} 
相關問題