Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在c中循环窗体#_C#_Loops_Input - Fatal编程技术网

C# 在c中循环窗体#

C# 在c中循环窗体#,c#,loops,input,C#,Loops,Input,如何使它在程序结束时请求另一个输入,以便表单重置,并根据新输入生成另一个表单 static void Main() { String totalsecondsstring; int totalseconds, hour, minutes, second; Console.WriteLine("How long did the race take (in seconds)?"); totalsecondsstring = 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");
    }
两项建议:

使用
while(true)
循环:

static void Main(string[] args) {
    while(true) {
        // Your code
    }
}
调用
Main
方法末尾的
Main
方法:

static void Main(string[] args) {
    // Your code
    Main();
}
我建议您使用第一种方法。

这里是一个示例

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

您应该使用以下循环之一:
for
foreach
while
,或
do…while
。关于更多细节,我建议阅读以下阅读材料:或

在编程中,在使用“表单”这个词时要小心。尤其是.NET编程。这不是一种形式。这是一个控制台应用程序。使用
console.Clear()
:)递归调用
Main
将最终导致
StackOverflowException
,所以绝对不要这样做。我同意@juharr。即使用户需要几天(可能甚至几年)才能完成,最好使用循环。此外,打破递归循环也会很麻烦,因为它需要你做一些事情,比如抛出异常来阻止它。你应该删除递归主解决方案,这不是一个好主意