【发布时间】:2015-04-29 10:27:25
【问题描述】:
我有这个代码来验证电话号码,但它看起来有点尴尬。我猜有更好的方法来解决这个问题。我怎样才能提高效率?
public static bool validTelephoneNo(string telNo)
{
bool condition = false;
while (condition == false)
{
Console.WriteLine("Enter a phone number.");
telNo = Console.ReadLine();
if (telNo.Length > 8)
{
if (telNo.StartsWith("+") == true)
{
char[] arr = telNo.ToCharArray();
for (int a = 1; a < telNo.Length; a++)
{
int temp;
try
{
temp = arr[a];
}
catch
{
break;
}
if (a == telNo.Length - 1)
{
condition = true;
}
}
}
}
}
return true;
}
【问题讨论】:
-
在我看来Code Review会更好..
-
什么使电话号码有效?
-
由整数组成,长度小于8位,开头有加号
-
电话号码的有效格式是什么?使用正则表达式。
标签: c#