2017-05-03 17 views
2

在觸發我的方法並將數據存儲到我的數據庫之前,我需要從控制檯應用程序中檢查一組用戶輸入。在觸發方法之前需要驗證一組用戶輸入c#

該程序編譯和rund沒有例外。但是如果有一個錯誤的輸入,其他三個輸入仍然會通過。
雖然,我真正需要的是在觸發方法之前確保4個用戶的條目是正確的,萬一只有一個錯誤,整個程序應該停止並退出。你可以採用

using System; 
using System.Threading; 

namespace BarcodeValidation 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      ReadBarcode(); 
     } 

     static void ReadBarcode() 
     { 
      var barcodes = GetInput(); 

      foreach (var item in barcodes) 
      { 
       // something 
       CheckUserInput(item); 
      } 
     } 

     static string[] GetInput() 
     { 
      Console.WriteLine("Please enter 4 products ID, Barcodes, MPN or EAN code:"); 

      string[] barcode = new string[4]; 

      for (int i = 0; i < barcode.Length; i++) 
      { 
       barcode[i] = Console.ReadLine(); 
      } 
      return barcode; 
     } // end of method here 

     static void CheckUserInput(string userInput) 
     { 
      int msec = 5000; 

      try 
      { 
       if (!(userInput == "F5121" || userInput == "F3111" || userInput == "F8331" || userInput == "F5321")) 
       { 
        Console.WriteLine("Enter a valid MPN codes for your products"); 
        Thread.Sleep(msec); 
        Environment.Exit(0); 
       } 
       else 
       { 
        switch (userInput) 
        { 
         case "F5121": 
          Console.WriteLine("barcode 1 is =", userInput); 
          Thread.Sleep(msec); 
          break; 
         case "F3111": 
          Console.WriteLine("barcode 2 is =", userInput); 
          Thread.Sleep(msec); 
          break; 
         case "F8331": 
          Console.WriteLine("barcode 3 is =", userInput); 
          Thread.Sleep(msec); 
          break; 
         case "F5321": 
          Console.WriteLine("barcode 4 is =", userInput); 
          break; 
        } 
       } 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex.Message); 
      } 
     } 
    } 
}  
+0

這不就是你在做什麼?爲每個項目檢查用戶輸入。如果輸入不正確,則CheckUserInput會退出。您當前的程序中有哪些不起作用? – Default

+0

'bool errorOccurred = false;'=>替換'foreach' w /'while(errorOccurred == false)',並且在輸入無效時將'errorOccurred'設置爲true? 檢查後,再看看是否繼續。 –

+0

你想「不運行」程序的哪一部分?因爲如果至少有一個條目不正確,它會退出程序,您已經擁有該權限。它會立即退出。但是,如果所有4都通過了「CheckUserInput」,那麼程序已經結束了,就像在「ReadBarcode」中發生的那樣......如果它們中的任何一個錯誤,你都不想運行什麼? – Skintkingle

回答

2

既然你有實際測試你的用戶輸入的方法使用它的返回值

static bool CheckUserInput(string userInput) // true : valid | false : invalid 
{ 
    int msec = 5000; 
    try 
    { 
     if (!(userInput == "F5121" || 
       userInput == "F3111" || 
       userInput == "F8331" || 
       userInput == "F5321")) 
     {  
      Console.WriteLine("Enter a valid MPN codes for your products"); 
      return false; 
     } 
     else 
     { 
      switch (userInput) 
      { 
       case "F5121": 
        Console.WriteLine("barcode 1 is =", userInput); 
        Thread.Sleep(msec); 
        return true;      
       case "F3111": 
        Console.WriteLine("barcode 2 is =", userInput); 
        Thread.Sleep(msec); 
        return true; 
       case "F8331": 
        Console.WriteLine("barcode 3 is =", userInput); 
        Thread.Sleep(msec); 
        return true; 
       case "F5321": 
        Console.WriteLine("barcode 4 is =", userInput); 
        return true; 
       default: 
        return false; 
      } 
     } 

    } 
    catch (Exception ex) 
    { 

     Console.WriteLine(ex.Message); 
     return false; 
    } 

} 

ReadBarcodes看起來是這樣的:

static void ReadBarcode() 
{ 
    var barcodes = GetInput(); 
    bool errorOccured = false; 
    foreach (var item in barcodes) 
    { 
     // something 
     if(!CheckUserInput(item)) 
     { 
      errorOccured = true; // keep track of that error 
      break; //Break for if 1 input is invalid 
     } 
    } 
    //Further execution.... 
    if(errorOccured) 
    { 
     return; //Do not continue ... 
    } 
    //Do other things you want to do. Your input is valid at this point ! 
} 

或更短喜歡默認報價:

static void ReadBarcode() 
{   
    if(!GetInput().All(CheckUserInput)) 
    { 
     return; 
    } 
    //Your stuff goes here. Input is valid at this point 
} 
+1

或使用LINQ:'bool success = barcodes.All(CheckUserInput);' – Default

+0

@默認好點!我將其包含在我的回答中 –

+0

也許在交換機中包含「default」值並將if檢查代碼移動到默認值時非常有用,如果它與任何情況都不匹配,則將其隱含。 – Skintkingle

0

一種選擇是創建自己的類,從System.Exception派生,並在其中輸入一個被發現是無效的情況下,你可以把你的異常類的實例。

您可以將代碼包裝在try-catch塊中,然後將補救代碼放入catch塊中。

0

您需要打破檢查ng代碼和「輸出」代碼放到不同的地方。您需要檢查所有值是否都是有效值。在你檢查完所有的值之後,再做你的console.writelines(這是你不想要發生的部分)。此時它會檢查一個並執行代碼(如果該代碼有效),然後轉到下一個代碼。 CheckUserInput應該只檢查用戶輸入,不應該根據該方法的結果做其他事情。你應該有CheckUserInput和例如ExecuteBarcodeStuff,並且僅當所有CheckUserInput迴歸真實的,你應該運行新的(尚未未實現)ExecuteBarcodeStuff

混合與其他民族的答案是做LINQ查詢或這種方法,例如,以確保所有結果是積極的比賽會得到你想要的結果。