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。即使用户需要几天(可能甚至几年)才能完成,最好使用循环。此外,打破递归循环也会很麻烦,因为它需要你做一些事情,比如抛出异常来阻止它。你应该删除递归主解决方案,这不是一个好主意