我刚刚开始了解C#代码,并且想要试用一个控制台应用程序。相当基本,并且会涉及一个循环来执行一些工作,直到用户决定退出。这是我的程序现在的样子。程序读取以前的输入
public void Method1(string[] args)
{
if (args.Length != 0)
{
DoWork(args);
ResetValues();
Loop(parameter);
}
else
{
Console.WriteLine("No arguments passed");
string helpMsg ...
Console.WriteLine(helpMsg);
}
public void Loop(parameter)
{
bool wantsContinue = true;
while (wantsContinue)
{
Console.WriteLine("What would you like to do now?\n-Exit\tWrite 'e'\n-Run again\tWrite 'r'");
ConsoleKeyInfo command = Console.ReadKey();
char key = command.KeyChar;
switch (key)
{
case 'e':
return;
case 'r':
Console.WriteLine("Please enter your commands");
string input = Console.ReadLine();
Method1(parameters);
break;
case 'h':
Console.WriteLine(helpMsg);
break;
default:
Console.WriteLine("\nInvalid argument. Enter again");
break;
}
}
}
public void MethodContinuous(input)
{
Console.WriteLine(input);
string[] args = input.Split(' ');
if (args.Length != 0)
{
DoWork(args);
}
else
{
Console.WriteLine("No arguments passed");
string helpMsg = ...
Console.WriteLine(helpMsg);
}
}
但是,我遇到了一个我无法弄清楚的问题。当程序第一次进入循环时,它会正确设置参数,但是当循环继续时,它会向我提供上一次运行的用户输入。我可能做的事情不对,或者控制台的工作方式有点不同。专家能解决它吗?
那么,什么是'ProgramLoop()'?什么是ResetValues()? – 2013-05-13 13:03:22
改变了它。基本上我设置了我要事先写的东西,但是我想我得到了一个解决方案。更改方法名称并忘记更新它们。 ProgramLoop被重命名为循环。 ResetValues只是重置我的变量的值,没有别的。正如你可以明显看出,我是一个初学者! :( – 2013-05-13 13:07:47
好的,没关系......但是,这些方法会发生什么?还有'DoWork()'中,你的'main'是什么样的?我相信你最终会杀死你的堆栈... – 2013-05-13 13:08:45