C# 过度使用中断语句的解决方案?
我有一个完全实用的程序,我现在正在重构它。我正处于学习c语言的过程中,所以原始代码非常糟糕,尽管它运行得很好。该程序的要求之一是用户能够在任何时候返回主菜单。我做到了以下几点:C# 过度使用中断语句的解决方案?,c#,C#,我有一个完全实用的程序,我现在正在重构它。我正处于学习c语言的过程中,所以原始代码非常糟糕,尽管它运行得很好。该程序的要求之一是用户能够在任何时候返回主菜单。我做到了以下几点: static bool bouncer = false static void Exit(string input) { if (input == "\t") { bouncer = true } } static string Prompt(string msg) { /
static bool bouncer = false
static void Exit(string input)
{
if (input == "\t")
{
bouncer = true
}
}
static string Prompt(string msg)
{
// takes input and passes it to Exit() then returns the input
}
static string FunctionA()
{
while(true)
{
if (bouncer == true)
{
break;
}
Prompt("whatever")
if (bouncer == true)
{
break;
}
Prompt("whatever")
if (bouncer == true)
{
break;
}
// return some stuff
}
}
static void Main()
{
bouncer = false
// writes the menu to console and handles UI
// FunctionA
{
如果用户在任何输入点输入“tab”字符,则变量bouncer将设置为true。break语句条件的激增提供了一种结构,这种结构实际上会分解回Main()。这显然不是一个很好的解决方案,它使代码很难阅读
我考虑完成相同任务的其他尝试有:
如有任何建议,我们将不胜感激。我希望在事件处理方面可以做些什么,或者我忽略了一些更简单的事情。谢谢 请尝试使用条件标志,而不是无限循环和中断语句
static void FunctionA()
{
bool done = false;
string response = string.Empty;
while (!done)
{
response = Prompt("whatever");
if(response == '\t')
{
done = true;
}
}
}
作为旁注,我不知道为什么在不使用返回值的情况下,将'string'作为几个方法(例如'function')的返回类型。这就是为什么我上面给出的代码被称为“void”。作为一种编码风格,它的工作方式,但被认为是丑陋的。不幸的是,如果你需要在工作的各个部分之间立即突破,没有很多方法可以解决这个问题 您可以将当前使用中断的格式更改为使用“
if(bContinue){/*do next section of work*/}
”控件样式。它将代码从中断while循环更改为:
static string FunctionA()
{
bool bContinue = true;
while( true == bContinue )
{
// Do initital work.
//
// Initial work can set bContinue to false if any error condition
// occurs.
if( true == bContinue )
{
// Do more work.
int returnCheck = MakeACall(); // Presume MakeACall returns negative interger values for error, 0 or positive values for success or success with condition/extra information.
if( 0 < returnCheck )
{
bContinue = false;
}
}
if( true == bContinue )
{
Prompt("whatever")
// Do more work.
bContinue = MakeASecondCall(); // Presume that MakeASecondCall returns true for success, false for error/failure
}
if( true == bContinue )
{
Prompt("whatever")
// Do more work.
// If error encountered, set bContinue to false.
}
if( true == bContinue )
{
Prompt("whatever else")
// Do more work.
// If error encountered, set bContinue to false.
}
// Done with loop, so drop out.
bContinue = false;
// return some stuff
}
}
静态字符串函数()
{
bool bContinue=true;
while(true==b继续)
{
//做一些基本的工作。
//
//如果出现任何错误条件,初始工作可以将bContinue设置为false
//发生。
if(true==bContinue)
{
//做更多的工作。
int returnCheck=MakeACall();//假定MakeACall返回负整数表示错误,0或正值表示成功或带条件/额外信息的成功。
如果(0<返回检查)
{
b继续=假;
}
}
if(true==bContinue)
{
提示(“任何”)
//做更多的工作。
bContinue=MakeASecondCall();//假设MakeASecondCall返回true表示成功,返回false表示错误/失败
}
if(true==bContinue)
{
提示(“任何”)
//做更多的工作。
//如果遇到错误,请将bccontinue设置为false。
}
if(true==bContinue)
{
提示(“其他任何内容”)
//做更多的工作。
//如果遇到错误,请将bccontinue设置为false。
}
//循环结束,所以退出。
b继续=假;
//还些东西
}
}
看看你的伪代码,它看起来就像你只在你的工作循环中做了一次传递。如果是这样,您可以切换到Do While(false)
格式,并使用分隔符将其放到底部。或者,如果您只通过函数执行一次操作,只需取消While
或do While
控制结构,只需使用if(true==bContinue){/*do more work*/}
。它不是最干净的代码,但是当您执行长时间的串行工作时,如果您不打算使用while
或do while
来控制流,那么您最终会得到这样的结构
使用if(bContinue){}
样式的缺点是,当在流程的早期阶段出现错误情况时,如果错误发生在工作顶部附近,则代码不会像中断那样从while()
结构或do-while()
结构中快速退出,因为将有一系列代码将测试然后跳过的if
语句。但是它是可读的,如果您为控制变量使用描述性名称(即,nContinue
或bContinue
或workLoopControl
),那么很明显,对于在您之后工作或查看代码的人来说,它是函数工作流的主控制标志。您可以尝试替代。谢谢。我不知道!这是一个控制台程序吗?如果是这样,为什么要从命令行实现菜单应用程序?Winform和Webforms都会使这成为一个问题。我以前从未使用过Winform,但现在我正在研究它。谢谢你指给我看!OP似乎刚刚注释掉了//在实际程序中返回一些东西的代码部分。我只是不想把它们写成虚空的代码。这个解决方案很有意义,尽管我的印象是while循环上的条件只会在循环的每次迭代中进行评估。不对吗?没错。如果您需要中断while循环的中间部分,您必须使用中断,正如您在问题中发布的代码示例中所示。@StarPilot这就是问题所在。好像