2015-02-11 48 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); 
     }