当我在循环中满足if条件时,我试图退出Dowhile循环。使用Dowhile循环的原因是tagIDNumber有时会占用垃圾值。这是我对代码的意图
但是tagIDNumber也可以是空字符串,在这种情况下,while循环必须退出。 当if条件满足时,下面的代码似乎不适用于退出dowhile循环。
public string TagID()
{
SetAttenuation(Attenuation);
StringBuilder tagIDNumber = new StringBuilder(); // TagID
int TagType;
TagType = Identify(tagIDNumber, SEGOTAGTYPE);
// When the application is launched first time, previous Tag ID value is empty
if (Globals.previoustagIDNumber != "")
{
if (tagIDNumber.ToString().ToUpper() != Globals.previoustagIDNumber)
{
do
{
tagIDNumber.Clear();
TagType = Identify(tagIDNumber, SEGOTAGTYPE);
if (tagIDNumber.ToString() == "")
{
return "NO TAG";
break;
}
}
while (tagIDNumber.ToString().ToUpper() != Globals.previoustagIDNumber);
}
return tagIDNumber.ToString().ToUpper();
}
else
{
if (TagType == SEGOTAGTYPE)
{
if (tagIDNumber.ToString() == "")
{
return "NO TAG";
}
else
{
return tagIDNumber.ToString().ToUpper();
}
}
else
{
return "RESTART";
}
}
}
答案 0 :(得分:0)
您可以使用变量指示do ...循环退出,而不是破坏代码的正常流程:
bool getOut=false;
do
{
tagIDNumber.Clear();
TagType = Identify(tagIDNumber, SEGOTAGTYPE);
if (tagIDNumber.ToString() == "")
getOut=true;
}
while (tagIDNumber.ToString().ToUpper() != Globals.previoustagIDNumber & getOut==false);