2012-01-20 140 views
1

我對字符串和int感到困惑。並且不能驗證名稱是沒有數字和奇怪的特徵。 a-z和A-Z都很好。我理解while循環和哨兵的使用。我在這裏看到了正則表達式,但由於某種原因,我的代碼無法工作。我寧願選擇一種我能理解的簡單解決方案。我在我的代碼中驗證int的效果很好,但驗證名稱會得到bool和int錯誤。字符串字母驗證

static void Main(string[] args) 
    { 
     int age; 
     double mileage; 
     string strInput, name; 
     bool isValid; 

     DisplayApplicationInformation(); 

     DisplayDivider("Start Program"); 
     Console.WriteLine(); 

     DisplayDivider("Get Name"); 
     strInput = GetInput("your name"); 
     name = strInput; 
     Console.WriteLine("Your name is: " + name); 
     Console.WriteLine(); 

     do 
     { 
     DisplayDivider("Get Age"); 
     strInput = GetInput("your age"); 
     isValid = int.TryParse(strInput, out age); 
     if (!isValid || (age <= 0)) 
     { 
      isValid = false; 
      Console.WriteLine("'" + strInput + "' is not a valid age entry. Please retry..."); 
     } 
     }while (!isValid); 
     Console.WriteLine("Your age is: " + age); 
     //age = int.Parse(strInput); 
     //Console.WriteLine("Your age is: " + age); 
     Console.WriteLine(); 

     do 
     { 
     DisplayDivider("Get Mileage"); 
     strInput = GetInput("gas mileage"); 
     isValid = double.TryParse(strInput, out mileage); 
     if (!isValid || (mileage <= 0)) 
     { 
      isValid = false; 
      Console.WriteLine("'" + strInput + "' is not a valid mileage entry. Please retry..."); 
     } 
     } while (!isValid); 
     Console.WriteLine("Your age is: " + mileage); 
     //mileage = double.Parse(strInput); 
     //Console.WriteLine("Your car MPT is: " + mileage); 

     TerminateApplication(); 
    } 
+0

向我們展示你試圖驗證名稱是什麼線。 – antlersoft

+0

你能進一步解釋你提到的錯誤嗎?你在代碼中得到了實際的例外嗎?如果是這樣,它們是什麼,它們在哪裏發生,以及它們發生時的數據狀態如何?如果沒有實際的錯誤,錯誤的指示是什麼?預期的行爲與觀察到的行爲有什麼不同?代碼中的哪兩個位置有偏差? – David

+4

如果你可以向自己介紹一下正則表達式,它比你試圖嘗試的簡單得多;) – craig1231

回答

0

下面是對工作的正則表達式,你想要什麼

 Regex reg = new Regex("^[A-Za-z]+$"); 
     do 
     { 
      DisplayDivider("Get Name"); 
      strInput = GetInput("your Name"); 
      isValid = reg.IsMatch(strInput); 
      if (!isValid) 
      { 
       isValid = false; 
       Console.WriteLine("'" + strInput + "' is not a valid name entry. Please retry..."); 
      } 
     } while (!isValid); 

基本上,它表示「匹配字符串的開頭(^表示開始)並且只有字母直到字符串的結尾($表示字符串的結尾)並且必須有一個或多個字母(即+)」

可能是最簡單的

現在,這是假設當然,你不希望「名姓」,因爲這將意味着有涉及的空間。讓我知道你是否需要分隔空間。

+0

我得到一個錯誤使用正則表達式: –

+0

無法找到類型或命名空間名稱'正則表達式'(您是否缺少使用指令或程序集引用?) –

+0

@badboy右鍵單擊「正則表達式」,然後將鼠標懸停在「解決」它應該顯示使用System.Text.RegularExpressions。添加該參考,你應該很好。 – Dan

5

儘管我會建議你使用一個簡單的正則表達式,但你指出你想要一個不同的解決方案。

看一看this question,第一個答案是正則表達式的解決方案,但second answer可能會回答你的問題:

bool result = input.All(Char.IsLetter); 

正如克里斯·萊弗利指出,如果允許在名稱中有空格,那麼你可以驗證具有:

bool result = input.Replace(" ", "").All(Char.IsLetter); 
+1

這將返回false,例如var input =「Marry Had Little Lamb」;空格不是字母 – MethodMan

+0

+1的答案@DJKRAZE - 這不是在規範中允許空格 –

+2

@DJKRAZE:在這種情況下,使用:bool result = input.Replace(「」,「」).All( Char.IsLetter);仍然比正則表達式更容易 – NotMe

0

壞小子@克里斯Lively的建議是更多你想要的東西容易閱讀,而不是正則表達式

bool result = strInput.Replace(" ", "").All(Char.IsLetter); 

    if you are wanting to do it the long way with a forloop then look at this example 

for (int i = 0; i < strinput.Length; i++) 
{ 
    //if this character isn't a letter and it isn't a Space then return false 
    //because it means this isn't a valid alpha string 
    if (!(char.IsLetter(strinput[i])) && (!(char.IsWhiteSpace(strinput[i])))) 
    return false; 
} 
+0

DisplayDivider(「Get Name」); strInput = GetInput(「你的名字」); bool result = strInput.Replace(「」,「」).All(Char.IsLetter); name = strInput; Console.WriteLine(「Your name is:」+ name); Console.WriteLine(); –

+0

我得到沒有錯誤,但數字仍然輸出 –

+0

你有沒有包裹如果只顯示名稱,只有當你需要在原始文章中以更可讀的方式發佈確切的代碼,你可以做到這一點 – MethodMan