文字列補間に対する解答を確認する
次のコードは、前のユニットの課題に対して考えられる解決策の 1 つです。
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);
ロジックの実装方法に大きく依存するため、このコードは単に "1 つの可能なソリューション" にすぎません。 このモジュールで取り上げる手法を使用して文字列の書式設定や文字列の埋め込みなどを行い、出力がチャレンジ出力と一致する限り、すばらしい結果が得られます。
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)などの複合書式設定も、考えられるもう 1 つの解決策です。ソリューションの 2 番目の部分では、文字列連結、複合書式設定を使用した
string.Format()、書式指定子 (パーセントと通貨)、およびPadRight()を使用して、長い文字列を段階的に構築して比較テーブルを表示します。次のコードは、文字列
comparisonMessageを構築する各ステップの後にConsole.WriteLine()を追加して、テーブルの最初の行をビルドします。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);次の出力例は、比較テーブルの最初の行が 3 つの手順でどのように構築されているかを示しています。
Magic Yield Magic Yield 12.75% Magic Yield 12.75% $55,000,000.00
この課題を完了できなかった場合は、先に進む前に、これまでのユニットを確認した方がよいと思われます。