C#重新启动控制台应用程序 [英] C# Restarting a console application
问题描述
我创建了一个小型应用程序,做一个小的转换。在节目的最后我创建了一个方法,允许用户进行其它计算,如果他们按R。我只希望它做的是,如果他们按R,带他们回到主的开始,否则终止程序。我不想使用goto语句。这是我到目前为止得到的,并且错误我得到。
I've created a small application that does a small conversion. At the end of the program I've created a method that allows the user to make another calculation if they press 'r'. All I want it to do is if they press r, take them back to the beginning of Main, else terminate program. I do not want to use goto. This is what I've got so far, and the error I'm getting.
的 http://puu.sh/juBWP/c7c3f7be61.png
推荐答案
while循环将是一个不错的选择,但既然你说该程序应该运行,然后给用户再次运行的选项,一个更好的循环将是一个DO WHILE。之间,而与执行虽然不同的是,你的虽然将始终运行至少一次。
A while loop would be a good fit, but since you say the program should run and then give the user the option to run again, an even better loop would be a Do While. The difference between while and Do While is that Do While will always run at least once.
string inputStr;
do
{
RunProgram();
Console.WriteLine("Run again?");
inputStr = Console.ReadLine();
} while (inputStr == "y");
TerminateProgram();
这篇关于C#重新启动控制台应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!