2015-02-11 43 views
1

如何在程序結束時使它成爲另一個輸入,以便表單重新設置,並根據新輸入創建另一個表單?在c中循環一個表格#

static void Main() 
    { 
     String totalsecondsstring; 
     int totalseconds, hour, minutes, second; 
     Console.WriteLine("How long did the race take (in seconds)?"); 
     totalsecondsstring = Console.ReadLine(); 
     totalseconds = Convert.ToInt32(totalsecondsstring); 
     hour = totalseconds/3600; 
     minutes = (totalseconds - (hour * 3600))/60; 
     second = (totalseconds - (hour * 3600)) - (minutes * 60); 
     Console.Write("Runers Time\n"); 
     Console.WriteLine("-----------\n"); 
     Console.WriteLine("{0,-5} | {1,-5} | {2,-5} | {3,-5}", "Runner ", hour + " hours", minutes + " minutes", second + " seconds"); 
    } 
+5

在編程時在「Form」單詞周圍晃動時要小心。 *尤其是* .NET編程。這不是一種形式。其控制檯應用程序。 – BradleyDotNET 2015-02-11 18:13:17

回答

1

兩個建議:

使用while(true)循環:

static void Main(string[] args) { 
    while(true) { 
     // Your code 
    } 
} 

呼叫在Main方法結束的Main方法:

static void Main(string[] args) { 
    // Your code 
    Main(); 
} 

我建議你使用第一種方法。

+0

謝謝!如何清除運行之間的表單? – 2015-02-11 18:13:43

+1

使用'Console.Clear()':) – 2015-02-11 18:14:03

+3

遞歸調用'Main'最終會導致一個'StackOverflowException',所以絕對不要這樣做。 – juharr 2015-02-11 18:16:46

1

下面是一個例子,

static void Main() 
     { 
      bool anotherRace; 

      do 
      { 
       Console.WriteLine("How long did the race take (in seconds)?"); 
       string totalsecondsstring = Console.ReadLine(); 

       int totalseconds = Convert.ToInt32(totalsecondsstring); 
       int hour = totalseconds/3600; 
       int minutes = (totalseconds - (hour * 3600))/60; 
       int second = (totalseconds - (hour * 3600)) - (minutes * 60); 

       Console.Write("Runers Time\n"); 
       Console.WriteLine("-----------\n"); 
       Console.WriteLine(
        "{0,-5} | {1,-5} | {2,-5} | {3,-5}", 
        "Runner ", 
        hour + " hours", 
        minutes + " minutes", 
        second + " seconds"); 

       Console.WriteLine("Would you like to do another race? y or n\n"); 

       anotherRace = Console.ReadLine() == "y"; 
      } 
      while (anotherRace); 
     }