查看字符串内插的解决方案
以下代码是上一单元中所述挑战的一种可能的解决方案。
string customerName = "Ms. Barros";
string currentProduct = "Magic Yield";
int currentShares = 2975000;
decimal currentReturn = 0.1275m;
decimal currentProfit = 55000000.0m;
string newProduct = "Glorious Future";
decimal newReturn = 0.13125m;
decimal newProfit = 63000000.0m;
Console.WriteLine($"Dear {customerName},");
Console.WriteLine($"As a customer of our {currentProduct} offering we are excited to tell you about a new financial product that would dramatically increase your return.\n");
Console.WriteLine($"Currently, you own {currentShares:N} shares at a return of {currentReturn:P}.\n");
Console.WriteLine($"Our new product, {newProduct} offers a return of {newReturn:P}. Given your current volume, your potential profit would be {newProfit:C}.\n");
Console.WriteLine("Here's a quick comparison:\n");
string comparisonMessage = "";
comparisonMessage = currentProduct.PadRight(20);
comparisonMessage += String.Format("{0:P}", currentReturn).PadRight(10);
comparisonMessage += String.Format("{0:C}", currentProfit).PadRight(20);
comparisonMessage += "\n";
comparisonMessage += newProduct.PadRight(20);
comparisonMessage += String.Format("{0:P}", newReturn).PadRight(10);
comparisonMessage += String.Format("{0:C}", newProfit).PadRight(20);
Console.WriteLine(comparisonMessage);
此代码只是“一种可能的解决方案”,因为它很大程度上取决于你决定实现逻辑的方式。 只要你使用此模块中介绍的技术来设置字符串、填充字符串等的格式,并且输出与质询输出匹配,那么你就做得很棒!
Dear Ms. Barros,
As a customer of our Magic Yield offering we are excited to tell you about a new financial product that would dramatically increase your return.
Currently, you own 2,975,000.00 shares at a return of 12.75%.
Our new product, Glorious Future offers a return of 13.13%. Given your current volume, your potential profit would be $63,000,000.00.
Here's a quick comparison:
Magic Yield 12.75% $55,000,000.00
Glorious Future 13.13% $63,000,000.00
如果成功了,恭喜你!
解决方案详细信息
可以继续本部分,了解给定解决方案如何解决此挑战。
花一分钟时间查看解决方案代码。
可以开始分解解决方案,并首先解决将问候语和打开段落写入终端的问题。 下面的代码可解决使用字符串内插显示问候语
Dear Ms. Barros,的问题。 现在应熟悉模式Console.WriteLine($"Your text {yourVariable}");:string customerName = "Ms. Barros"; Console.WriteLine($"Dear {customerName},");代码输出为:
Dear Ms. Barros,再次查看示例完整解决方案。 解决方案的前半部分使用字符串内插来显示第一段的每个部分。
注释
复合格式设置,例如
Console.WriteLine("Dear {0},", customerName)另一种可能的解决方案。解决方案的第二部分通过使用字符串串联
string.Format()、复合格式、格式说明符(百分比和货币)PadRight()逐步生成长字符串来显示比较表。以下代码生成表的第一行,并添加
Console.WriteLine()每个生成字符串comparisonMessage的步骤后。string currentProduct = "Magic Yield"; int currentShares = 2975000; decimal currentReturn = 0.1275m; decimal currentProfit = 55000000.0m; string comparisonMessage = ""; comparisonMessage = currentProduct.PadRight(20); Console.WriteLine(comparisonMessage); comparisonMessage += String.Format("{0:P}", currentReturn).PadRight(10); Console.WriteLine(comparisonMessage); comparisonMessage += String.Format("{0:C}", currentProfit).PadRight(20); Console.WriteLine(comparisonMessage);以下示例输出显示了如何通过三个步骤生成比较表的第一行。
Magic Yield Magic Yield 12.75% Magic Yield 12.75% $55,000,000.00
如果在完成此项挑战时遇到问题,可能需要先回顾前面的几个单元,然后再继续。