2012-01-20 164 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