练习 - 编写代码以读取和保存新的“我们的动物”数组数据
在本练习中,你将为每个宠物特征开发数据输入验证循环,然后存储新的 ourAnimals 数组数据。 在本练习中完成的详细任务包括:
- 读取和验证物种:构建一个循环和内部代码结构,用于输入和验证宠物物种。
- 构造宠物 ID:编写使用 petCount 和物种名称构造 petID 值的代码。
- 读取和验证年龄:生成一个循环和内部代码结构,用于输入和验证宠物的年龄。
- 读取并验证物理说明:生成循环和内部代码结构,用于输入宠物的物理说明。
- 阅读并验证个性描述:生成循环和内部代码结构,用于输入宠物个性的描述。
- 读取并验证昵称:生成循环和内部代码结构,用于输入宠物的昵称。
- 验证测试:对在本练习中开发的代码执行验证测试。
重要
在开始本练习之前,必须先完成本模块中的上一个练习。
生成循环来读取和验证宠物物种
在此任务中,你将创建一个 do 循环,该循环会重复执行直到用户输入有效的物种名称( 狗 或 猫)。 重复使用可撤销的字符串 readResult 来捕获 Console.ReadLine() 输入。 还可以重复使用 animalSpecies 生成示例数据时使用的字符串变量。 向应用添加新的 validEntry 布尔变量。 在计算得到 validEntry 循环的退出条件的表达式中使用 do。
确保 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();若要在赋值
readResultreadResult之前确保值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 中打开集成终端面板,并输入用于生成程序的命令。
如果报告了任何生成错误或警告,请修复问题,然后再继续。
构造动物 ID 值
在此任务中,您使用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();若要在赋值
readResultreadResult之前确保该值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 生成示例数据时使用的字符串变量。 在计算得出 animalPhysicalDescription 循环的退出条件的表达式中使用赋给 do 的值。
在年龄数据输入循环的代码块下方添加一个空白代码行。
若要为物理说明数据输入创建
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();若要在赋值
readResultreadResult之前确保该值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 生成示例数据时使用的字符串变量。 在计算得出 animalPersonalityDescription 循环的退出条件的表达式中使用赋给 do 的值。
在物理说明数据输入循环的代码块下方添加一个空白代码行。
若要为个性描述数据条目创建循环
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();若要在赋值
readResultreadResult之前确保该值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 生成示例数据时使用的字符串变量。 在计算得出 animalNickname 循环的退出条件的表达式中使用赋给 do 的值。
在个性描述数据输入循环的代码块下方添加一个空白代码行。
若要为个性描述数据条目创建循环
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();若要在赋值
readResultreadResult之前确保该值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提示符处,输入“狗” - 在
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如果未显示新添加的宠物信息,请检查确保已包含代码行以将数据保存到 ourAnimals 数组,并检查确保已包含代码行来构造 petID。
验证是否可以为狗和猫创建其他动物说明,以及动物特征是否保存到数组中
ourAnimals。退出应用程序,然后关闭终端面板。
祝贺你完成此指导项目! 你已创建一个应用程序,该应用程序结合了选择和迭代语句,以实现应用程序设计目标。 应用程序包括 300 多行,并执行可在专业应用程序中找到的任务。 完成此项目是一项重大成就。 请再接再厉!