練習 - 撰寫程式碼以讀取和儲存新的ourAnimals陣列數據
在此練習中,您將開發針對每個寵物特性的驗證迴圈,然後儲存新的 ourAnimals 陣列數據。 在此練習期間完成的詳細工作如下:
- 讀取和驗證物種:建立迴圈,以及用來輸入和驗證寵物物種的內部程式代碼結構。
- 建構寵物標識碼:撰寫使用 petCount 和物種名稱來建構 petID 值的程式代碼。
- 讀取和驗證年齡:建立迴圈和內部程式代碼結構,用來輸入和驗證寵物的年齡。
- 讀取並驗證實體描述:建置迴圈,以及用來輸入寵物實體描述的內部程式代碼結構。
- 閱讀並驗證個性描述:建立迴圈和內部程式代碼結構,用來輸入寵物個性的描述。
- 讀取並驗證暱稱:建置迴圈,以及用來輸入寵物昵稱的內部程式代碼結構。
- 驗證測試: 針對您在此練習中開發的程式碼執行驗證測試。
這很重要
您必須先完成本課程模組中的上一個練習,才能開始此練習。
建置循環來讀取和驗證寵物物種
在這項工作中,您會建立一個 do 迴圈,該迴圈會持續執行,直到使用者輸入有效的物種名稱,即 狗 或 貓。 您可以重複使用無效的字串 readResult 來擷取 Console.ReadLine() 輸入。 您也可以重複使用 animalSpecies 產生範例數據時所使用的字串變數。 您會將名為 validEntry 的新布林變數新增至您的應用程式。 您在評估為 do 迴圈允出準則的運算式中使用 validEntry。
確定 Visual Studio Code 已開啟,且您的Program.cs檔案會顯示在編輯器中。
找出
while (anotherPet == "y" && petCount < maxPets)指令,然後在頂端程式區塊插入一個空白行。在您建立的空白程式代碼行上,若要宣告
validEntry初始值為false的 ,請輸入下列程式代碼:bool validEntry = false;在 宣告
validEntry下方的 行上,若要建立do物種數據輸入的迴圈,請輸入下列程序代碼:// get species (cat or dog) - string animalSpecies is a required field do { } while (validEntry == false);在
do語句的代碼區塊內,若要建立顯示提示並讀取使用者輸入,請輸入下列代碼:Console.WriteLine("\n\rEnter 'dog' or 'cat' to begin a new entry"); readResult = Console.ReadLine();若要在將
readResult的值指派給readResult之前,確定animalSpecies的值不是 null,請輸入下列程式碼:if (readResult != null) { animalSpecies = readResult.ToLower(); }在值指派下方
animalSpecies的這一行上,若要確保animalSpecies包含有效的物種名稱,請輸入下列程序代碼:if (animalSpecies != "dog" && animalSpecies != "cat") { validEntry = false; } else { validEntry = true; }比較已完成的物種名稱資料輸入迴圈與下列程式代碼:
// 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);在 [Visual Studio Code 檔案] 功能表上,選取 [儲存]。
在 Visual Studio Code 中開啟 [整合式終端機] 面板,然後輸入命令以建置程式。
如果報告了任何建置錯誤或警告,請先修正問題再繼續。
建構動物標識碼值
在這項工作中,您會使用 animalSpecies 和 petCount 變數來建立您指派給 animalID的值。
在物種名稱數據輸入迴圈的程式代碼區塊下方新增空白代碼行。
若要建立並指派
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();在 [Visual Studio Code 檔案] 功能表上,選取 [儲存]。
建置循環來讀取和驗證寵物的年齡
在這項工作中,您會建立 do 迴圈,逐一查看直到使用者輸入 ? 或代表寵物年齡 (以年為單位) 的有效整數為止。 您可以重複使用無效字串 readResult 來擷取 Console.ReadLine() 輸入。 您也可以重複使用 animalAge 產生範例數據時所使用的字串變數。 若要測試字串是否 animalAge 代表有效的整數,您可以使用 validEntry 布爾值。 您可以宣告名為 petAge 的新整數變數來儲存數值。 同樣地,validEntry 布林值會用於評估為 do 迴圈允出準則的運算式。
在行下方新增空白程式代碼行,以將值指派給變數
animalID。若要建立存留期資料輸入的
do循環,請輸入下列程式代碼:// get the pet's age. can be ? at initial entry. do { } while (validEntry == false);在
do語句的程式碼區塊中,若要宣告一個名為petAge的整數變數,請輸入以下程式碼:int petAge;在 宣告
petAge下方的 這一行上,若要顯示訊息提示並讀取使用者輸入,請輸入下列程序代碼:Console.WriteLine("Enter the pet's age or enter ? if unknown"); readResult = Console.ReadLine();為了確保在將
readResult的值指派給readResult之前animalAge不為 null,請輸入下列程式碼:if (readResult != null) { animalAge = readResult; }在
animalAge值指派這一行的下方,若要在測試有效的整數之前檢查使用者是否輸入?,請輸入下列程式碼。if (animalAge != "?") { validEntry = int.TryParse(animalAge, out petAge); } else { validEntry = true; }比較已完成的年齡資料輸入迴圈與下列程式代碼:
// 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);在 [Visual Studio Code 檔案] 功能表上,選取 [儲存]。
在 Visual Studio Code 中開啟 [整合式終端機] 面板,然後輸入命令以建置程式。
如果報告了任何建置錯誤或警告,請先修正問題再繼續。
建置循環來讀取和驗證寵物的實體描述
在這項工作中,您會建立一個迴圈 do ,並反覆運行,直到使用者輸入代表寵物外觀描述的字串值為止。 您可以重複使用無效字串 readResult 來擷取 Console.ReadLine() 輸入。 您也可以重複使用 animalPhysicalDescription 產生範例數據時所使用的字串變數。 您會在評估為 do 迴圈允出準則的運算式中使用指派給 animalPhysicalDescription 的值。
在年齡數據輸入迴圈的程式代碼區塊下方新增空白代碼行。
若要建立實體描述數據輸入的
do迴圈,請輸入下列程序代碼:// get a description of the pet's physical appearance/condition - animalPhysicalDescription can be blank. do { } while (animalPhysicalDescription == "");在
do語句的代碼區塊內,若要建立顯示提示並讀取使用者輸入,請輸入下列代碼:Console.WriteLine("Enter a physical description of the pet (size, color, gender, weight, housebroken)"); readResult = Console.ReadLine();為了確保在將
readResult的值指派給readResult之前animalPhysicalDescription不為 null,請輸入下列程式碼:if (readResult != null) { animalPhysicalDescription = readResult.ToLower(); }若要在輸入的值為
"tbd"時,將的值animalPhysicalDescription指派給"",請輸入下列程式代碼:if (animalPhysicalDescription == "") { animalPhysicalDescription = "tbd"; }比較已完成的實體描述數據輸入迴圈與下列程式代碼:
// 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 == "");在 [Visual Studio Code 檔案] 功能表上,選取 [儲存]。
在 Visual Studio Code 中開啟 [整合式終端機] 面板,然後輸入命令以建置程式。
如果報告了任何建置錯誤或警告,請先修正問題再繼續。
建置循環來讀取和驗證寵物的個性描述
在這項任務中,您會建立一個迴圈 do ,反覆執行,直到使用者輸入代表寵物個性描述的字符串值為止。 您可以重複使用無效字串 readResult 來擷取 Console.ReadLine() 輸入。 您也可以重複使用 animalPersonalityDescription 產生範例數據時所使用的字串變數。 您會在評估為 do 迴圈允出準則的運算式中使用指派給 animalPersonalityDescription 的值。
在實體描述數據輸入迴圈的程式代碼區塊下方新增空白程式代碼行。
若要建立個人描述數據輸入的
do迴圈,請輸入下列程式代碼:// get a description of the pet's personality - animalPersonalityDescription can be blank. do { } while (animalPersonalityDescription == "");在
do語句的代碼區塊內,若要建立顯示提示並讀取使用者輸入,請輸入下列代碼:Console.WriteLine("Enter a description of the pet's personality (likes or dislikes, tricks, energy level)"); readResult = Console.ReadLine();為了確保在將
readResult的值指派給readResult之前animalPersonalityDescription不為 null,請輸入下列程式碼:if (readResult != null) { animalPersonalityDescription = readResult.ToLower(); }若要在輸入的值為
"tbd"時,將的值animalPersonalityDescription指派給"",請輸入下列程式代碼:if (animalPersonalityDescription == "") { animalPersonalityDescription = "tbd"; }比較已完成的個性描述資料輸入迴圈與下列程式代碼:
// 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 == "");在 [Visual Studio Code 檔案] 功能表上,選取 [儲存]。
在 Visual Studio Code 中開啟 [整合式終端機] 面板,然後輸入命令以建置程式。
如果報告了任何建置錯誤或警告,請先修正問題再繼續。
建置迴圈來讀取和驗證寵物的昵稱
在這項任務中,您會建立一個 do 迴圈,進行迭代,直到使用者輸入代表寵物昵稱的字串值為止。 您可以重複使用無效字串 readResult 來擷取 Console.ReadLine() 輸入。 您也可以重複使用 animalNickname 產生範例數據時所使用的字串變數。 您會在評估為 do 迴圈允出準則的運算式中使用指派給 animalNickname 的值。
在個性描述數據輸入迴圈的程式代碼區塊下方新增空白代碼行。
若要建立個人描述數據輸入的
do迴圈,請輸入下列程式代碼:// get the pet's nickname. animalNickname can be blank. do { } while (animalNickname == "");在
do語句的代碼區塊內,若要建立顯示提示並讀取使用者輸入,請輸入下列代碼:Console.WriteLine("Enter a nickname for the pet"); readResult = Console.ReadLine();為了確保在將
readResult的值指派給readResult之前animalNickname不為 null,請輸入下列程式碼:if (readResult != null) { animalNickname = readResult.ToLower(); }若要在輸入的值為
"tbd"時,將的值animalNickname指派給"",請輸入下列程式代碼:if (animalNickname == "") { animalNickname = "tbd"; }將已完成的暱稱數據輸入迴圈與下列程式代碼進行比較:
// 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 == "");在 [Visual Studio Code 檔案] 功能表上,選取 [儲存]。
在 Visual Studio Code 中開啟 [整合式終端機] 面板,然後輸入命令以建置程式。
如果報告了任何建置錯誤或警告,請先修正問題再繼續。
儲存新的寵物資訊
在這項任務中,您會將針對寵物特性輸入的值儲存至 ourAnimals 陣列。
在暱稱數據輸入迴圈的程式碼區塊下方新增空白代碼行。
若要儲存使用者指定的數據值,請輸入下列程式代碼:
// 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;在 [Visual Studio Code 檔案] 功能表上,選取 [儲存]。
在 Visual Studio Code 中開啟 [整合式終端機] 面板,然後輸入命令以建置程式。
如果報告了任何建置錯誤或警告,請先修正問題再繼續。
檢查您的工作
在這項工作中,您會從整合式終端機執行應用程式,並確認寵物數據輸入正常運作。
如有必要,請開啟 Visual Studio Code 的 [整合式終端] 面板。
在終端機命令提示字元中,輸入 dotnet run
在終端機命令提示字元中,輸入 2
確認終端機面板已更新以顯示下列輸出:
We currently have 4 pets that need homes. We can manage 4 more. Enter 'dog' or 'cat' to begin a new entry在終端機命令提示符號中輸入以下值,並確認每個後續提示符號是否顯示。
- 在
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)- 在
在終端機命令提示字元中,輸入 n
確認 [終端機] 面板已更新以顯示主功能表選項。
在終端機命令提示字元中,輸入 1
確認終端機面板已更新以顯示下列輸出:
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。
確認您可以為狗和貓建立其他動物描述,而且動物特性會儲存到
ourAnimals陣列中。結束應用程式,然後關閉終端機面板。
恭喜您完成此引導式專案! 您已建立結合選取和反覆專案語句的應用程式,以達成應用程式設計目標。 您的應用程式包含超過 300 行,並執行您在專業應用程式中可能找到的工作。 完成此專案代表重大成就。 跟上偉大的工作!