2013-04-23 84 views
3

任何人都可以請幫忙,因爲我在這裏遇到了一堵牆,我該如何檢測何時由於無效值而跳出循環。當出現循環時檢測到

例如,如果某人輸入年齡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; 
       } 
      } 

所以,我願意這樣做soemthing像

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

希望有人能幫助,因爲我已經是一片空白

喬治

+0

嗨@George你試圖使用調試器? visual studio有一個grat調試器。 – Nahum 2013-04-23 13:24:04

+0

難道你不能拖延'checkAgeOfChildren'全局並在循環結束後檢查它的值嗎? – 2013-04-23 13:24:31

+0

'if(!string。IsNullOrEmpty(問題))' – 2013-04-23 13:24:42

回答

5

很簡單,您已經自己給出答案:

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; 
} 
+0

嗨巴特,認爲我一直在看着屏幕很長,隨着布爾,因爲我幾乎在那裏開始,謝謝。我現在要休息 – 2013-04-23 13:32:35

+1

休息時經常給出最好的想法。祝你好運 ;-)。 – 2013-04-23 13:33:20

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); 
     } 
} 
+0

例外解決方案的一個例子會很好。我認爲這個例外很適合。 – 2013-04-23 13:24:45

+0

@ErikvanBrakel - 更新:) – keyboardP 2013-04-23 13:26:11

1

爲什麼don'y你只是檢查string.Empty在結束了嗎?如果你有一個錯誤,只會設置:

if (!string.IsNullOrEmpty(problem)) 
{ 
    ViewBag.Message = problem; 
} 
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可以用更合適的替代或自定義異常。

1

爲什麼不能在循環裏是這樣的:

if (checkAgeOfChildren == -1) 
       { 
        ViewBag.Message = "problem with age, stop checking"; 
        break; 
       } 
相關問題