練習 - 撰寫程式碼以讀取和儲存新的ourAnimals陣列數據

已完成

在此練習中,您將開發針對每個寵物特性的驗證迴圈,然後儲存新的 ourAnimals 陣列數據。 在此練習期間完成的詳細工作如下:

  1. 讀取和驗證物種:建立迴圈,以及用來輸入和驗證寵物物種的內部程式代碼結構。
  2. 建構寵物標識碼:撰寫使用 petCount 和物種名稱來建構 petID 值的程式代碼。
  3. 讀取和驗證年齡:建立迴圈和內部程式代碼結構,用來輸入和驗證寵物的年齡。
  4. 讀取並驗證實體描述:建置迴圈,以及用來輸入寵物實體描述的內部程式代碼結構。
  5. 閱讀並驗證個性描述:建立迴圈和內部程式代碼結構,用來輸入寵物個性的描述。
  6. 讀取並驗證暱稱:建置迴圈,以及用來輸入寵物昵稱的內部程式代碼結構。
  7. 驗證測試: 針對您在此練習中開發的程式碼執行驗證測試。

這很重要

您必須先完成本課程模組中的上一個練習,才能開始此練習。

建置循環來讀取和驗證寵物物種

在這項工作中,您會建立一個 do 迴圈,該迴圈會持續執行,直到使用者輸入有效的物種名稱,即 。 您可以重複使用無效的字串 readResult 來擷取 Console.ReadLine() 輸入。 您也可以重複使用 animalSpecies 產生範例數據時所使用的字串變數。 您會將名為 validEntry 的新布林變數新增至您的應用程式。 您在評估為 do 迴圈允出準則的運算式中使用 validEntry

  1. 確定 Visual Studio Code 已開啟,且您的Program.cs檔案會顯示在編輯器中。

  2. 找出 while (anotherPet == "y" && petCount < maxPets) 指令,然後在頂端程式區塊插入一個空白行。

  3. 在您建立的空白程式代碼行上,若要宣告 validEntry 初始值為 false的 ,請輸入下列程式代碼:

    bool validEntry = false;
    
    
  4. 在 宣告 validEntry下方的 行上,若要建立 do 物種數據輸入的迴圈,請輸入下列程序代碼:

    // get species (cat or dog) - string animalSpecies is a required field 
    do
    {
    } while (validEntry == false);
    
    
  5. do 語句的代碼區塊內,若要建立顯示提示並讀取使用者輸入,請輸入下列代碼:

    Console.WriteLine("\n\rEnter 'dog' or 'cat' to begin a new entry");
    readResult = Console.ReadLine();
    
    
  6. 若要在將readResult的值指派給readResult之前,確定animalSpecies的值不是 null,請輸入下列程式碼:

    if (readResult != null)
    {
        animalSpecies = readResult.ToLower();
    
    }
    
  7. 在值指派下方 animalSpecies 的這一行上,若要確保 animalSpecies 包含有效的物種名稱,請輸入下列程序代碼:

    if (animalSpecies != "dog" && animalSpecies != "cat")
    {
        validEntry = false;
    }
    else
    {
        validEntry = true;
    }
    
  8. 比較已完成的物種名稱資料輸入迴圈與下列程式代碼:

    // get species (cat or dog) - string animalSpecies is a required field 
    do
    {
        Console.WriteLine("\n\rEnter 'dog' or 'cat' to begin a new entry");
        readResult = Console.ReadLine();
        if (readResult != null)
        {
            animalSpecies = readResult.ToLower();
            if (animalSpecies != "dog" && animalSpecies != "cat")
            {
                //Console.WriteLine($"You entered: {animalSpecies}.");
                validEntry = false;
            }
            else
            {
                validEntry = true;
            }
        }
    } while (validEntry == false);
    
  9. 在 [Visual Studio Code 檔案] 功能表上,選取 [儲存]。

  10. 在 Visual Studio Code 中開啟 [整合式終端機] 面板,然後輸入命令以建置程式。

    如果報告了任何建置錯誤或警告,請先修正問題再繼續。

建構動物標識碼值

在這項工作中,您會使用 animalSpeciespetCount 變數來建立您指派給 animalID的值。

  1. 在物種名稱數據輸入迴圈的程式代碼區塊下方新增空白代碼行。

  2. 若要建立並指派 animalID 值,請輸入下列程序代碼:

    // build the animal the ID number - for example C1, C2, D3 (for Cat 1, Cat 2, Dog 3)
    animalID = animalSpecies.Substring(0, 1) + (petCount + 1).ToString();
    
    
  3. 在 [Visual Studio Code 檔案] 功能表上,選取 [儲存]。

建置循環來讀取和驗證寵物的年齡

在這項工作中,您會建立 do 迴圈,逐一查看直到使用者輸入 ? 或代表寵物年齡 (以年為單位) 的有效整數為止。 您可以重複使用無效字串 readResult 來擷取 Console.ReadLine() 輸入。 您也可以重複使用 animalAge 產生範例數據時所使用的字串變數。 若要測試字串是否 animalAge 代表有效的整數,您可以使用 validEntry 布爾值。 您可以宣告名為 petAge 的新整數變數來儲存數值。 同樣地,validEntry 布林值會用於評估為 do 迴圈允出準則的運算式。

  1. 在行下方新增空白程式代碼行,以將值指派給變數 animalID

  2. 若要建立存留期資料輸入的 do 循環,請輸入下列程式代碼:

    // get the pet's age. can be ? at initial entry. 
    do
    {
    } while (validEntry == false);
    
    
  3. do 語句的程式碼區塊中,若要宣告一個名為 petAge 的整數變數,請輸入以下程式碼:

    int petAge;
    
    
  4. 在 宣告 petAge下方的 這一行上,若要顯示訊息提示並讀取使用者輸入,請輸入下列程序代碼:

    Console.WriteLine("Enter the pet's age or enter ? if unknown");
    readResult = Console.ReadLine();
    
    
  5. 為了確保在將 readResult 的值指派給 readResult 之前 animalAge 不為 null,請輸入下列程式碼:

    if (readResult != null)
    {
        animalAge = readResult;
    
    }
    
  6. animalAge值指派這一行的下方,若要在測試有效的整數之前檢查使用者是否輸入?,請輸入下列程式碼。

    if (animalAge != "?")
    {
        validEntry = int.TryParse(animalAge, out petAge);
    }
    else
    {
        validEntry = true;
    }
    
  7. 比較已完成的年齡資料輸入迴圈與下列程式代碼:

    // get the pet's age. can be ? at initial entry.
    do
    {
        int petAge;
        Console.WriteLine("Enter the pet's age or enter ? if unknown");
        readResult = Console.ReadLine();
        if (readResult != null)
        {
            animalAge = readResult;
            if (animalAge != "?")
            {
                validEntry = int.TryParse(animalAge, out petAge);
            }
            else
            {
                validEntry = true;
            }
        }
    } while (validEntry == false);
    
  8. 在 [Visual Studio Code 檔案] 功能表上,選取 [儲存]。

  9. 在 Visual Studio Code 中開啟 [整合式終端機] 面板,然後輸入命令以建置程式。

    如果報告了任何建置錯誤或警告,請先修正問題再繼續。

建置循環來讀取和驗證寵物的實體描述

在這項工作中,您會建立一個迴圈 do ,並反覆運行,直到使用者輸入代表寵物外觀描述的字串值為止。 您可以重複使用無效字串 readResult 來擷取 Console.ReadLine() 輸入。 您也可以重複使用 animalPhysicalDescription 產生範例數據時所使用的字串變數。 您會在評估為 do 迴圈允出準則的運算式中使用指派給 animalPhysicalDescription 的值。

  1. 在年齡數據輸入迴圈的程式代碼區塊下方新增空白代碼行。

  2. 若要建立實體描述數據輸入的 do 迴圈,請輸入下列程序代碼:

    // get a description of the pet's physical appearance/condition - animalPhysicalDescription can be blank.
    do
    {
    } while (animalPhysicalDescription == "");
    
    
  3. do 語句的代碼區塊內,若要建立顯示提示並讀取使用者輸入,請輸入下列代碼:

    Console.WriteLine("Enter a physical description of the pet (size, color, gender, weight, housebroken)");
    readResult = Console.ReadLine();
    
    
  4. 為了確保在將 readResult 的值指派給 readResult 之前 animalPhysicalDescription 不為 null,請輸入下列程式碼:

    if (readResult != null)
    {
        animalPhysicalDescription = readResult.ToLower();
    
    }
    
  5. 若要在輸入的值為 "tbd"時,將的值animalPhysicalDescription指派給 "" ,請輸入下列程式代碼:

    if (animalPhysicalDescription == "")
    {
        animalPhysicalDescription = "tbd";
    }
    
  6. 比較已完成的實體描述數據輸入迴圈與下列程式代碼:

    // get a description of the pet's physical appearance/condition - animalPhysicalDescription can be blank.
    do
    {
        Console.WriteLine("Enter a physical description of the pet (size, color, gender, weight, housebroken)");
        readResult = Console.ReadLine();
        if (readResult != null)
        {
            animalPhysicalDescription = readResult.ToLower();
            if (animalPhysicalDescription == "")
            {
                animalPhysicalDescription = "tbd";
            }
        }
    } while (animalPhysicalDescription == "");
    
  7. 在 [Visual Studio Code 檔案] 功能表上,選取 [儲存]。

  8. 在 Visual Studio Code 中開啟 [整合式終端機] 面板,然後輸入命令以建置程式。

    如果報告了任何建置錯誤或警告,請先修正問題再繼續。

建置循環來讀取和驗證寵物的個性描述

在這項任務中,您會建立一個迴圈 do ,反覆執行,直到使用者輸入代表寵物個性描述的字符串值為止。 您可以重複使用無效字串 readResult 來擷取 Console.ReadLine() 輸入。 您也可以重複使用 animalPersonalityDescription 產生範例數據時所使用的字串變數。 您會在評估為 do 迴圈允出準則的運算式中使用指派給 animalPersonalityDescription 的值。

  1. 在實體描述數據輸入迴圈的程式代碼區塊下方新增空白程式代碼行。

  2. 若要建立個人描述數據輸入的 do 迴圈,請輸入下列程式代碼:

    // get a description of the pet's personality - animalPersonalityDescription can be blank.
    do
    {
    } while (animalPersonalityDescription == "");
    
    
  3. do 語句的代碼區塊內,若要建立顯示提示並讀取使用者輸入,請輸入下列代碼:

    Console.WriteLine("Enter a description of the pet's personality (likes or dislikes, tricks, energy level)");
    readResult = Console.ReadLine();
    
    
  4. 為了確保在將 readResult 的值指派給 readResult 之前 animalPersonalityDescription 不為 null,請輸入下列程式碼:

    if (readResult != null)
    {
        animalPersonalityDescription = readResult.ToLower();
    
    }
    
  5. 若要在輸入的值為 "tbd"時,將的值animalPersonalityDescription指派給 "" ,請輸入下列程式代碼:

    if (animalPersonalityDescription == "")
    {
        animalPersonalityDescription = "tbd";
    }
    
  6. 比較已完成的個性描述資料輸入迴圈與下列程式代碼:

    // get a description of the pet's personality - animalPersonalityDescription can be blank.
    do
    {
        Console.WriteLine("Enter a description of the pet's personality (likes or dislikes, tricks, energy level)");
        readResult = Console.ReadLine();
        if (readResult != null)
        {
            animalPersonalityDescription = readResult.ToLower();
            if (animalPersonalityDescription == "")
            {
                animalPersonalityDescription = "tbd";
            }
        }
    } while (animalPersonalityDescription == "");
    
  7. 在 [Visual Studio Code 檔案] 功能表上,選取 [儲存]。

  8. 在 Visual Studio Code 中開啟 [整合式終端機] 面板,然後輸入命令以建置程式。

    如果報告了任何建置錯誤或警告,請先修正問題再繼續。

建置迴圈來讀取和驗證寵物的昵稱

在這項任務中,您會建立一個 do 迴圈,進行迭代,直到使用者輸入代表寵物昵稱的字串值為止。 您可以重複使用無效字串 readResult 來擷取 Console.ReadLine() 輸入。 您也可以重複使用 animalNickname 產生範例數據時所使用的字串變數。 您會在評估為 do 迴圈允出準則的運算式中使用指派給 animalNickname 的值。

  1. 在個性描述數據輸入迴圈的程式代碼區塊下方新增空白代碼行。

  2. 若要建立個人描述數據輸入的 do 迴圈,請輸入下列程式代碼:

    // get the pet's nickname. animalNickname can be blank.
    do
    {
    } while (animalNickname == "");
    
    
  3. do 語句的代碼區塊內,若要建立顯示提示並讀取使用者輸入,請輸入下列代碼:

    Console.WriteLine("Enter a nickname for the pet");
    readResult = Console.ReadLine();
    
    
  4. 為了確保在將 readResult 的值指派給 readResult 之前 animalNickname 不為 null,請輸入下列程式碼:

    if (readResult != null)
    {
        animalNickname = readResult.ToLower();
    
    }
    
  5. 若要在輸入的值為 "tbd"時,將的值animalNickname指派給 "" ,請輸入下列程式代碼:

    if (animalNickname == "")
    {
        animalNickname = "tbd";
    }
    
  6. 將已完成的暱稱數據輸入迴圈與下列程式代碼進行比較:

    // get the pet's nickname. animalNickname can be blank.
    do
    {
        Console.WriteLine("Enter a nickname for the pet");
        readResult = Console.ReadLine();
        if (readResult != null)
        {
            animalNickname = readResult.ToLower();
            if (animalNickname == "")
            {
                animalNickname = "tbd";
            }
        }
    } while (animalNickname == "");
    
  7. 在 [Visual Studio Code 檔案] 功能表上,選取 [儲存]。

  8. 在 Visual Studio Code 中開啟 [整合式終端機] 面板,然後輸入命令以建置程式。

    如果報告了任何建置錯誤或警告,請先修正問題再繼續。

儲存新的寵物資訊

在這項任務中,您會將針對寵物特性輸入的值儲存至 ourAnimals 陣列。

  1. 在暱稱數據輸入迴圈的程式碼區塊下方新增空白代碼行。

  2. 若要儲存使用者指定的數據值,請輸入下列程式代碼:

    // store the pet information in the ourAnimals array (zero based)
    ourAnimals[petCount, 0] = "ID #: " + animalID;
    ourAnimals[petCount, 1] = "Species: " + animalSpecies;
    ourAnimals[petCount, 2] = "Age: " + animalAge;
    ourAnimals[petCount, 3] = "Nickname: " + animalNickname;
    ourAnimals[petCount, 4] = "Physical description: " + animalPhysicalDescription;
    ourAnimals[petCount, 5] = "Personality: " + animalPersonalityDescription;
    
  3. 在 [Visual Studio Code 檔案] 功能表上,選取 [儲存]。

  4. 在 Visual Studio Code 中開啟 [整合式終端機] 面板,然後輸入命令以建置程式。

    如果報告了任何建置錯誤或警告,請先修正問題再繼續。

檢查您的工作

在這項工作中,您會從整合式終端機執行應用程式,並確認寵物數據輸入正常運作。

  1. 如有必要,請開啟 Visual Studio Code 的 [整合式終端] 面板。

  2. 在終端機命令提示字元中,輸入 dotnet run

  3. 在終端機命令提示字元中,輸入 2

  4. 確認終端機面板已更新以顯示下列輸出:

    We currently have 4 pets that need homes. We can manage 4 more.
    
    Enter 'dog' or 'cat' to begin a new entry
    
  5. 在終端機命令提示符號中輸入以下值,並確認每個後續提示符號是否顯示。

    • Enter 'dog' or 'cat' to begin a new entry 提示中,輸入 dog
    • 在提示字元 Enter the pet's age or enter ? if unknown 中,輸入
    • Enter a physical description of the pet (size, color, gender, weight, housebroken) 提示字元中,按 Enter 鍵。
    • Enter a description of the pet's personality (likes or dislikes, tricks, energy level) 提示字元中,按 Enter 鍵。
    • Enter a nickname for the pet 提示字元中,按 Enter 鍵。

    終端機面板應該更新如下:

    Enter 'dog' or 'cat' to begin a new entry
    dog
    Enter the pet's age or enter ? if unknown
    ?
    Enter a physical description of the pet (size, color, gender, weight, housebroken)
    
    Enter a description of the pet's personality (likes or dislikes, tricks, energy level)
    
    Enter a nickname for the pet
    
    Do you want to enter info for another pet (y/n)
    
  6. 在終端機命令提示字元中,輸入 n

  7. 確認 [終端機] 面板已更新以顯示主功能表選項。

  8. 在終端機命令提示字元中,輸入 1

  9. 確認終端機面板已更新以顯示下列輸出:

    ID #: d1
    Species: dog
    Age: 2
    Nickname: lola
    Physical description: medium sized cream colored female golden retriever weighing about 65 pounds. housebroken.
    Personality: loves to have her belly rubbed and likes to chase her tail. gives lots of kisses.
    
    ID #: d2
    Species: dog
    Age: 9
    Nickname: loki
    Physical description: large reddish-brown male golden retriever weighing about 85 pounds. housebroken.
    Personality: loves to have his ears rubbed when he greets you at the door, or at any time! loves to lean-in and give doggy hugs.
    
    ID #: c3
    Species: cat
    Age: 1
    Nickname: Puss
    Physical description: small white female weighing about 8 pounds. litter box trained.
    Personality: friendly
    
    ID #: c4
    Species: cat
    Age: ?
    Nickname:
    Physical description:
    Personality:
    
    ID #: d5
    Species: dog
    Age: ?
    Nickname: tbd
    Physical description: tbd
    Personality: tbd
    Press the Enter key to continue
    

    如果未顯示新新增的寵物資訊,請檢查以確定您已包含程式代碼行,以將數據儲存至我們的Animals 陣列,並檢查以確定您已包含程式代碼行來建構 petID。

  10. 確認您可以為狗和貓建立其他動物描述,而且動物特性會儲存到 ourAnimals 陣列中。

  11. 結束應用程式,然後關閉終端機面板。

恭喜您完成此引導式專案! 您已建立結合選取和反覆專案語句的應用程式,以達成應用程式設計目標。 您的應用程式包含超過 300 行,並執行您在專業應用程式中可能找到的工作。 完成此專案代表重大成就。 跟上偉大的工作!