练习 - 创建可重用的方法
- 10 分钟
开发应用程序时,你可能会发现自己编写代码来反复执行相同的作。 使用方法执行相同的任务可缩短代码并帮助你更快地开发应用程序,而不是编写重复的代码。 在本练习中,你将识别重复的代码,并将其替换为可重用的方法。 让我们开始吧!
标识重复的代码
在此任务中,你将了解一个应用程序,用于跟踪不同时区的服药时间。 用户输入其当前时区及其目标时区。 他们的药物计划显示,然后调整为新的时区。
在 Visual Studio Code 编辑器中,删除前面练习中的任何现有代码。
将以下代码复制并粘贴到 Visual Studio Code 编辑器中:
using System; int[] times = {800, 1200, 1600, 2000}; int diff = 0; Console.WriteLine("Enter current GMT"); int currentGMT = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Current Medicine Schedule:"); /* Format and display medicine times */ foreach (int val in times) { string time = val.ToString(); int len = time.Length; if (len >= 3) { time = time.Insert(len - 2, ":"); } else if (len == 2) { time = time.Insert(0, "0:"); } else { time = time.Insert(0, "0:0"); } Console.Write($"{time} "); } Console.WriteLine(); Console.WriteLine("Enter new GMT"); int newGMT = Convert.ToInt32(Console.ReadLine()); if (Math.Abs(newGMT) > 12 || Math.Abs(currentGMT) > 12) { Console.WriteLine("Invalid GMT"); } else if (newGMT <= 0 && currentGMT <= 0 || newGMT >= 0 && currentGMT >= 0) { diff = 100 * (Math.Abs(newGMT) - Math.Abs(currentGMT)); /* Adjust the times by adding the difference, keeping the value within 24 hours */ for (int i = 0; i < times.Length; i++) { times[i] = ((times[i] + diff)) % 2400; } } else { diff = 100 * (Math.Abs(newGMT) + Math.Abs(currentGMT)); /* Adjust the times by adding the difference, keeping the value within 24 hours */ for (int i = 0; i < times.Length; i++) { times[i] = ((times[i] + diff)) % 2400; } } Console.WriteLine("New Medicine Schedule:"); /* Format and display medicine times */ foreach (int val in times) { string time = val.ToString(); int len = time.Length; if (len >= 3) { time = time.Insert(len - 2, ":"); } else if (len == 2) { time = time.Insert(0, "0:"); } else { time = time.Insert(0, "0:0"); } Console.Write($"{time} "); } Console.WriteLine();请注意,有多个
for-loop 重复相同的代码。有两
foreach个循环可以设置格式并显示医学时间。 另外还有两for个循环根据时区差异调整时间。编写代码时,你可能会发现自己重复执行相同任务的代码块。 这是使用方法执行任务来合并代码的绝佳机会。 让我们来练习一下!
创建执行重复任务的方法
确定重复代码后,可以创建一个方法来包含代码并删除重复代码。 使用方法增加了缩短代码并提高可读性的好处! 循环 foreach 格式并显示时间值,因此你可以为方法提供一个清楚地反映该任务的名称。 可以使用调整时间的 for 循环执行相同的作。 让我们开始吧!
在上一个代码的末尾输入新的空白代码行。
在新的空白代码行上,输入以下代码来创建方法签名:
void DisplayTimes() { }若要定义方法正文,请复制并粘贴
DisplayTimes块来更新foreach方法,如下所示:void DisplayTimes() { /* Format and display medicine times */ foreach (int val in times) { string time = val.ToString(); int len = time.Length; if (len >= 3) { time = time.Insert(len - 2, ":"); } else if (len == 2) { time = time.Insert(0, "0:"); } else { time = time.Insert(0, "0:0"); } Console.Write($"{time} "); } Console.WriteLine(); }在此方法中,你将在显示时间后添加对末尾的调用
Console.WriteLine以追加一个新行。 接下来,你将创建另一种方法来根据时区差异调整时间。在上一个代码的末尾输入新的空白代码行。
在新的空白代码行上,输入以下代码来创建方法签名:
void AdjustTimes() { }AdjustTimes通过复制并粘贴for循环来更新方法,如下所示:void AdjustTimes() { /* Adjust the times by adding the difference, keeping the value within 24 hours */ for (int i = 0; i < times.Length; i++) { times[i] = ((times[i] + diff)) % 2400; } }
步骤 3:调用方法
在此任务中,你将删除重复的代码块,并将其替换为对所创建方法的调用。
在注释“格式和显示医学时间”下找到重复
foreach循环的第一个实例:Console.WriteLine("Current Medicine Schedule:"); /* Format and display medicine times */ foreach (int val in times) { ... } Console.WriteLine(); Console.WriteLine("Enter new GMT");将标识的代码替换为对方法的
DisplayTimes调用。 替换应生成以下代码:Console.WriteLine("Current Medicine Schedule:"); DisplayTimes(); Console.WriteLine("Enter new GMT");接下来,你将替换重复代码的第二个实例。
在注释“格式并显示医学时间”下找到循环的第
foreach二个实例:Console.WriteLine("New Medicine Schedule:"); /* Format and display medicine times */ foreach (int val in times) { ... } Console.WriteLine();将标识的代码替换为对方法的
DisplayTimes调用。 替换应生成以下代码:Console.WriteLine("New Medicine Schedule:"); DisplayTimes();请注意如何使用方法代替大型代码块,从而更清晰地了解代码,并使代码更易于理解。 让我们对
AdjustTimes创建的方法执行相同的作。使用重复
for的 -loops 找到以下代码:else if (newGMT <= 0 && currentGMT <= 0 || newGMT >= 0 && currentGMT >= 0) { diff = 100 * (Math.Abs(newGMT) - Math.Abs(currentGMT)); /* Adjust the times by adding the difference, keeping the value within 24 hours */ for (int i = 0; i < times.Length; i++) { times[i] = ((times[i] + diff)) % 2400; } } else { diff = 100 * (Math.Abs(newGMT) + Math.Abs(currentGMT)); /* Adjust the times by adding the difference, keeping the value within 24 hours */ for (int i = 0; i < times.Length; i++) { times[i] = ((times[i] + diff)) % 2400; } }将注释下的重复代码实例替换为对
AdjustTimes方法的调用“通过添加差异来调整时间”。 替换应生成以下代码:else if (newGMT <= 0 && currentGMT <= 0 || newGMT >= 0 && currentGMT >= 0) { diff = 100 * (Math.Abs(newGMT) - Math.Abs(currentGMT)); AdjustTimes(); } else { diff = 100 * (Math.Abs(newGMT) + Math.Abs(currentGMT)); AdjustTimes(); }现在,所有重复的代码都已替换为新方法。 请注意代码看起来更具可读性和简洁性!
检查你的工作
在此任务中,从集成终端运行应用程序,并验证代码是否正常工作。 让我们开始吧。
将代码与以下内容进行比较,确保其正确:
int[] times = {800, 1200, 1600, 2000}; int diff = 0; Console.WriteLine("Enter current GMT"); int currentGMT = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Current Medicine Schedule:"); DisplayTimes(); Console.WriteLine("Enter new GMT"); int newGMT = Convert.ToInt32(Console.ReadLine()); if (Math.Abs(newGMT) > 12 || Math.Abs(currentGMT) > 12) { Console.WriteLine("Invalid GMT"); } else if (newGMT <= 0 && currentGMT <= 0 || newGMT >= 0 && currentGMT >= 0) { diff = 100 * (Math.Abs(newGMT) - Math.Abs(currentGMT)); AdjustTimes(); } else { diff = 100 * (Math.Abs(newGMT) + Math.Abs(currentGMT)); AdjustTimes(); } Console.WriteLine("New Medicine Schedule:"); DisplayTimes(); void DisplayTimes() { /* Format and display medicine times */ foreach (int val in times) { string time = val.ToString(); int len = time.Length; if (len >= 3) { time = time.Insert(len - 2, ":"); } else if (len == 2) { time = time.Insert(0, "0:"); } else { time = time.Insert(0, "0:0"); } Console.Write($"{time} "); } Console.WriteLine(); } void AdjustTimes() { /* Adjust the times by adding the difference, keeping the value within 24 hours */ for (int i = 0; i < times.Length; i++) { times[i] = ((times[i] + diff)) % 2400; } }使用 Ctrl + S 或使用 Visual Studio Code 文件菜单保存工作。
如有必要,打开 Visual Studio Code 的集成终端面板。
在“EXPLORER”面板中,若要在 TestProject 文件夹位置打开终端,请右键单击“TestProject”,然后选择“在集成终端中打开”。
在终端命令提示符处,输入 dotnet run
为 GMT 提示输入 -6 和 +6。
验证代码是否生成以下输出:
Enter current GMT -6 Current Medicine Schedule: 8:00 12:00 16:00 20:00 Enter new GMT +6 New Medicine Schedule: 20:00 0:00 4:00 8:00如果代码显示不同的结果,则需要查看代码以查找错误并进行更新。 再次运行代码以查看是否已解决问题。 继续更新并运行代码,直到代码生成预期结果。