4

任何人都可以帮忙,因为我在这里遇到了一堵墙,我如何检测我何时因无效值而跳出循环。

例如,如果有人为孩子输入年龄 3 5 6 7 9 就可以了,如果输入 o 3 5 6 7 这将在我的代码中返回 -1 并退出循环。

我如何检测到并返回消息

public static int IntIsValid(string p)
        {
            int pn;
            bool isNum = int.TryParse(p, out pn);
            int pnIsvalid = isNum ? pn : -1;
            return pnIsvalid;
        }

string addDash  = Regex.Replace(agesOfChildren, " ", "_");
            string[] splitNumberOfChildren  = addDash.Split('_');
            string splitChildrensAge        = string.Empty;
            int checkAgeOfChildren          = 0;
            string problem = string.Empty;
            foreach (var splitAgeOfChild in splitNumberOfChildren)
            {
                splitChildrensAge           = splitAgeOfChild;
                checkAgeOfChildren          = RemoveInvalidInput.IntIsValid(splitChildrensAge);
                if (checkAgeOfChildren == -1)
                {
                    problem = "problem with age, stop checking";
                    break;
                }
            }

所以我想做一些像

if(error with loop == true)
{
ViewBag.Message = problem;
}

希望有人可以提供帮助,因为我已经空白了

乔治

4

5 回答 5

6

很简单,你已经自己给出了答案:

boolean error_with_loop = false;
foreach (var splitAgeOfChild in splitNumberOfChildren) {
      splitChildrensAge           = splitAgeOfChild;
      checkAgeOfChildren          = RemoveInvalidInput.IntIsValid(splitChildrensAge);
      if (checkAgeOfChildren == -1)
      {
           error_with_loop = true;
           problem = "problem with age, stop checking";
           break;
      }
}

if (error_with_loop) {
    ViewBag.Message = problem;
}

或者你抛出一个Exception

try {
    foreach (var splitAgeOfChild in splitNumberOfChildren) {
          splitChildrensAge           = splitAgeOfChild;
          checkAgeOfChildren          = RemoveInvalidInput.IntIsValid(splitChildrensAge);
          if (checkAgeOfChildren == -1)
          {
                // note: no need to break
                throw new ErrorWithLoopException();
          }
    }
} catch (ErrorWithLoopException e) {
    ViewBag.Message = problem;
}

事实上,您似乎使用了某种整数验证。该Int32课程涵盖了例外情况:http: //msdn.microsoft.com/en-us/library/system.int32.parse%28v=vs.71%29.aspx您的代码实际上会更短(不知道如果适用,因为我不知道您的验证代码有什么额外功能):

try {
    foreach (var splitAgeOfChild in splitNumberOfChildren) {
          splitChildrensAge           = splitAgeOfChild;
          checkAgeOfChildren          = Int32.Parse(splitChildrensAge);
    }
} catch (FormatException e) {
    ViewBag.Message = problem;
}
于 2013-04-23T13:23:53.397 回答
2

您可以抛出异常(取决于您的程序设计),也可以设置一个标志。

bool success = true;

foreach (var splitAgeOfChild in splitNumberOfChildren)
{
      splitChildrensAge = splitAgeOfChild;
      checkAgeOfChildren = RemoveInvalidInput.IntIsValid(splitChildrensAge);
      if (checkAgeOfChildren == -1)
      {
           problem = "problem with age, stop checking";
           success = false; // change the flag
           break;
      }
}

或者使用异常(我在这里使用了ArgumentOutOfRangeException但您可以创建自己的):

foreach (var splitAgeOfChild in splitNumberOfChildren)
{
      splitChildrensAge = splitAgeOfChild;
      checkAgeOfChildren = RemoveInvalidInput.IntIsValid(splitChildrensAge);
      if (checkAgeOfChildren == -1)
      {
           problem = "problem with age, stop checking";
           throw new ArgumentOutOfRangeException("Age", problem);
      }
}
于 2013-04-23T13:24:02.593 回答
1

你为什么不检查string.Empty最后?仅当您有错误时才会设置它:

if (!string.IsNullOrEmpty(problem))
{
     ViewBag.Message = problem;
}
于 2013-04-23T13:25:07.817 回答
1

只需这样做:

if (checkAgeOfChildren == -1)
{
  problem = "problem with age, stop checking";
  break;
}

if (!string.IsNullOrEmpty(problem)) {
   ViewBag.Message = problem;
}

你也可以抛出异常:

try { 
   if (checkAgeOfChildren == -1)
   {
      throw new ArgumentException("Invalid Argument");
   }  catch (ArgumentException e) 
   {
       ViewBag.Message = e.Message;
   }
}

ArgumentException可以替换为更合适或自定义的异常。

于 2013-04-23T13:25:10.693 回答
1

为什么不在循环内这样:

 if (checkAgeOfChildren == -1)
                {
                    ViewBag.Message  = "problem with age, stop checking";
                    break;
                }
于 2013-04-23T13:28:58.343 回答