簡體   English   中英

驗證電話號碼C#

[英]Validating Phone Numbers C#

給出一個電話號碼列表,假設電話號碼是91 12 34 56,是不是可以正確撥打電話? 因為緊急線路。

我正在嘗試構建一個程序,如果用戶輸入以911開頭的電話號碼,輸出應該是“不一致”,否則“一致”。

這是我的代碼:

static void Main(string[] args)
{
    var phoneList = new List<string>();
    string input;
    Console.WriteLine("The Phone Number: ");

    while ((input = Console.ReadLine()) != null)
    {
        phoneList.Add(input);
    }

    for (int i = 0; i < phoneList.Count; i++)
    {
        for (int j = 0; j < phoneList.Count; j++)
        {
            if (phoneList[i].Substring(0, 3).Equals(phoneList[j].Substring(0, 3)) && i != j)
            {
                Console.WriteLine("NOT CONSISTENT");
                return;
            }
        }
    }
    Console.WriteLine("CONSISTENT");
}

我輸入911時,我的程序就跳過if語句。為什么會這樣?

編輯:電話號碼也是最多十位數的序列!

你走在正確的軌道上。 你在代碼中遇到了一些錯誤,無論是邏輯錯誤還是結構錯誤。

首先,你的while循環繼續下去,因為它一直持續到輸入為空......這是不可能發生的 - 最多,輸入將是一個空字符串""

其次,你要檢查字符串中輸入的前三個數字(好)與列表中每個字符串的前三個數字(不好)。 相反,你應該檢查"911"

static void Main(string[] args)
{
    var phoneList = new List<string>();
    string input;
    Console.WriteLine("The Phone Number: ");

    while ((input = Console.ReadLine()) != "")
    {
        phoneList.Add(input);
    }    

    for (int i = 0; i < phoneList.Count; i++)
    {
        if (phoneList[i].Substring(0, 3) == "911")
        {
            Console.WriteLine("NOT CONSISTENT");
            return;
        }
    }
    Console.WriteLine("CONSISTENT");
}

但是,重要的是要注意,此代碼不檢查任何特殊字符,空格等...它假設輸入將類似於1234567890而不是12 34 56 78 90 如果要刪除所有空格,請確保在每個輸入上使用String.Replace(" ", "") ,然后再通過for循環運行它們。

永遠不會達到你的for循環,因為你的程序無法離開while循環(輸入永遠不會為null)。

此外,如果它將到達for循環,則算法不正確:它將每個電話號碼的前三個數字與到目前為止收集的電話號碼列表中的其他電話號碼的所有前三個數字進行比較。

完成任務的更好方法是檢查前三個字符是否不等於“991”。 您可以添加一個方法,例如名為IsValidPhoneNumber 之后可以擴展此方法以包含更多有效性檢查(因為這非常復雜,您可以使用像GlobalPhone這樣的庫來執行此操作)。 接下來,我們只在電話列表中添加一個號碼,如果它有效。

由於您的程序不會輕易退出while循環(輸入永遠不會為null),您可能希望在某些時候對您的數字執行某些操作。 為此,我添加了鍵入“PRINT”的功能,然后將添加到目前為止的列表。

private const string PRINT_PHONE_NUMBERS_IDENTIFIER = "PRINT";
private static readonly List<string> PhoneList = new List<string>();

static void Main(string[] args)
{
    Console.WriteLine($"Add phone numbers to phone list (or {PRINT_PHONE_NUMBERS_IDENTIFIER} to print list so far): ");

    string input;
    while ((input = Console.ReadLine()) != null) // Keeps reading since input will never be null.
    {
        if (input == PRINT_PHONE_NUMBERS_IDENTIFIER)
        {
            PrintAllNumbers();
        }
        else if (IsValidPhoneNumber(input))
        {
            Console.WriteLine("CONSISTENT");
            PhoneList.Add(input);
        }
        else
        {
            Console.WriteLine("NOT CONSISTENT");
        }
    }
}

private static bool IsValidPhoneNumber(string phoneNumber)
{
    return !phoneNumber.StartsWith("991");
}

private static void PrintAllNumbers()
{
    if (PhoneList.Any())
    {
        foreach (var phoneNumber in PhoneList)
        {
            Console.WriteLine(phoneNumber);
        }
    }
    else
    {
        Console.WriteLine("Phone list is empty");
    }
}

將電話號碼添加到電話列表(或打印到目前為止打印列表):
打印
電話列表為空
991
不一致
打印
電話列表為空
1234567890
一致的
1235432665
一致的
打印
1234567890
1235432665

我已經把它帶走了,但你可以通過將功能重構為一個單獨的類來進一步改進程序。 然后,您可以圍繞它構建辦公應用程序。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM