练习 - 查看起始代码
在开发过程的这一第一步中,你将查看 Starter 项目文件夹中提供的代码。
查看 Program.cs 文件的内容
Program.cs文件包含你正在处理的应用程序的初步版本。 该代码包括生成和显示应用程序示例数据的功能,并显示用于定义应用程序主要功能的菜单选项列表。
确保在 Visual Studio Code 中打开“GuidedProject”文件夹(Guided-project-Work-with-variable-data-in-CSharp-main)。
“准备”单元(本模块中的上一个单元)包含一个安装程序部分,描述下载引导项目的初始起点以及打开文件夹 Visual Studio Code 的过程。 如有必要,请返回并按照设置说明进行操作。
在“资源管理器”视图中,展开“初学者”文件夹,然后选择“Program.cs”。
选择 Program.cs 文件时,文件内容将在资源管理器右侧的主编辑器区域中打开。
如果未打开资源管理器视图,可以从 Visual Studio Code 最左侧的活动栏中选择/打开资源管理器视图。 EXPLORER 是活动栏上最顶部的图标。
花几分钟时间查看Program.cs文件顶部的初始变量声明。
// #1 the ourAnimals array will store the following: string animalSpecies = ""; string animalID = ""; string animalAge = ""; string animalPhysicalDescription = ""; string animalPersonalityDescription = ""; string animalNickname = ""; // #2 variables that support data entry int maxPets = 8; string? readResult; string menuSelection = ""; // #3 array used to store runtime data, there is no persisted data string[,] ourAnimals = new string[maxPets, 6];首先,会看到注释(注释 #1)后跟变量列表。 这些变量从
animalSpecies到animalNickname用于在名为ourAnimals的多维字符串数组中保存宠物特征的值,并初始化为包含一个零长度的字符串""。ourAnimals数组声明在代码靠后的位置。下一组变量(注释 #2 下)是用于帮助生成示例数据、读取用户输入和为主程序循环建立退出条件的变量组合
stringint。 请注意代码行string? readResult;。 可以使用?字符来转换通常不可为 null 的变量类型(int、string、bool 等),并支持可以为 null 的类型。注释
使用
Console.ReadLine()该方法读取用户输入的值时,最好启用可为 null 的类型字符串string?,以避免生成项目时的代码编译器生成警告。最后一个变量(在 comment #3 下)是名为
ourAnimals二维字符串数组。 已将 maxPets 定义的行数初始化为 8。 最初存储的特征数为 6。 这六个特征与在示例代码中检查的字符串变量数匹配,但特征数需要扩展才能添加字段suggestedDonation。向下滚动 Program.cs 文件以检查其代码块中包含
for选择构造的switch循环。该代码示例是一个缩短的版本,用于节省空间。
// #4 create sample data ourAnimals array entries for (int i = 0; i < maxPets; i++) { switch (i) { case 0: animalSpecies = "dog"; animalID = "d1"; animalAge = "2"; animalPhysicalDescription = "medium sized cream colored female golden retriever weighing about 45 pounds. housebroken."; animalPersonalityDescription = "loves to have her belly rubbed and likes to chase her tail. gives lots of kisses."; animalNickname = "lola"; break; case 1: animalSpecies = "dog"; animalID = "d2"; animalAge = "9"; animalPhysicalDescription = "large reddish-brown male golden retriever weighing about 85 pounds. housebroken."; animalPersonalityDescription = "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."; animalNickname = "gus"; break; // case 2: deleted for brevity // case 3: deleted for brevity default: animalSpecies = ""; animalID = ""; animalAge = ""; animalPhysicalDescription = ""; animalPersonalityDescription = ""; animalNickname = ""; break; } ourAnimals[i, 0] = "ID #: " + animalID; ourAnimals[i, 1] = "Species: " + animalSpecies; ourAnimals[i, 2] = "Age: " + animalAge; ourAnimals[i, 3] = "Nickname: " + animalNickname; ourAnimals[i, 4] = "Physical description: " + animalPhysicalDescription; ourAnimals[i, 5] = "Personality: " + animalPersonalityDescription; }请注意,
for循环使用maxPets变量在注释 #4 后的迭代数上建立上限。另请注意,构造
switch有选择地对代码进行分支,以便可以在示例数据集中为宠物定义不同的宠物特征。使用
switch语句为for循环的前四次迭代定义不同的值。 在示例数据处理后,所有特征均为空或零长度字符串。动物特征变量的值在
for循环的底部被分配给 ourAnimals 数组。滚动到 Visual Studio Code 中代码文件的底部。 检查用于显示菜单选项并捕获用户选择输入的代码。
应观察以下代码:
// #5 display the top-level menu options do { // NOTE: the Console.Clear method is throwing an exception in debug sessions Console.Clear(); Console.WriteLine("Welcome to the Contoso PetFriends app. Your main menu options are:"); Console.WriteLine(" 1. List all of our current pet information"); Console.WriteLine(" 2. Display all dogs with a specified characteristic"); Console.WriteLine(); Console.WriteLine("Enter your selection number (or type Exit to exit the program)"); readResult = Console.ReadLine(); if (readResult != null) { menuSelection = readResult.ToLower(); } // use switch-case to process the selected menu option switch (menuSelection) { case "1": // list all pet info for (int i = 0; i < maxPets; i++) { if (ourAnimals[i, 0] != "ID #: ") { Console.WriteLine(); for (int j = 0; j < 6; j++) { Console.WriteLine(ourAnimals[i, j]); } } } Console.WriteLine("\n\rPress the Enter key to continue"); readResult = Console.ReadLine(); break; case "2": // Display all dogs with a specified characteristic"); Console.WriteLine("\nUNDER CONSTRUCTION - please check back next month to see progress."); Console.WriteLine("Press the Enter key to continue."); readResult = Console.ReadLine(); break; default: break; } } while (menuSelection != "exit");请花一分钟查看这两条
case语句。只有两个菜单选项可用于有限版本的应用程序。 初学者应用程序仅提供运行和测试功能原型所需的功能。
请注意,代码行
readResult = Console.ReadLine();后跟一个 null 值检查。使用
Console.ReadLine()方法的代码将值设置为可为null的字符串readResult,以避免在构建项目时由编译器生成警告。
检查你的工作
在 TERMINAL 命令提示符处测试初学者代码控制台应用,通过输入一个命令来构建并运行项目代码:
dotnet run。注释
终端提示必须在开始文件夹中打开,且终端应类似于
..\ArrayGuidedProject\starter>代码运行时,将显示两个菜单项。
- 输入:
1,以测试“列出我们当前所有宠物信息”的输出。 - 输入
2来测试占位符消息“正在构造中”消息
- 输入:
输入:
1,然后按下“Enter”键来“显示所有宠物”。检查是否显示所有宠物信息。
观察所有宠物的数据时,显示的最后一只宠物应与以下输出匹配:
ID #: c4 Species: cat Age: 3 Nickname: Lion Physical description: Medium sized, long hair, yellow, female, about 10 pounds. Uses litter box. Personality: A people loving cat that likes to sit on your lap. Press the Enter key to continue按 Enter 键继续并返回到菜单。
在菜单提示符下,输入
2后按“Enter”键。此选项是“显示所有具有指定特征的狗”功能的占位符。
检查是否在选择
UNDER CONSTRUCTION后显示了Display all dogs with a specified characteristic消息。应该会看到以下输出:
UNDER CONSTRUCTION - please check back next month to see progress. Press the Enter key to continue.在应用菜单中键入
exit来结束程序,然后关闭终端面板。此时程序应退出。
现在,你已准备好开始开发新功能。