檢閱字串插補的解決方案

已完成

下列程式碼是先前單元中挑戰的其中一個可能解決方案。

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  

如果您已成功,恭喜!

解決方案詳細資料

您可以繼續本節,以取得特定解決方案如何解決這項挑戰的說明。

  1. 請花一分鐘的時間檢閱解決方案的程式碼。

    您可以開始細分解決方案,並先解決於終端寫入問候語和開啟段落的問題。 下列程式碼會使用字串插補,解決顯示問候語 Dear Ms. Barros, 的問題。 您現在應該熟悉模式 Console.WriteLine($"Your text {yourVariable}");

    string customerName = "Ms. Barros";
    Console.WriteLine($"Dear {customerName},");
    

    程式碼輸出為:

    Dear Ms. Barros,   
    

    再次檢閱範例完整解決方案。 解決方案的前半部會使用字串插補來顯示第一個段落的每個部分。

    注意

    複合格式設定,例如 Console.WriteLine("Dear {0},", customerName) 是另一種可能的解決方案。

  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);
    

    下列範例輸出顯示如何在三個步驟中,建置比較資料表的第一行。

    Magic Yield
    Magic Yield         12.75%
    Magic Yield         12.75%    $55,000,000.00
    

如果您無法順利完成此挑戰,您應該在繼續之前先複習上一個單元中的內容。