Xem lại giải pháp cho nội suy chuỗi

Hoàn thành

Mã sau đây là một giải pháp khả thi cho thử thách từ đơn vị trước đó.

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

Mã này chỉ đơn thuần là "một giải pháp khả thi" vì rất nhiều tùy thuộc vào cách bạn quyết định thực thi lô-gic. Miễn là bạn đã sử dụng các kỹ thuật được đề cập trong mô-đun này để định dạng chuỗi, chuỗi pad, v.v. và đầu ra phù hợp với đầu ra thử thách, sau đó bạn đã làm rất tốt!

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  

Nếu bạn thành công, xin chúc mừng!

Chi tiết giải pháp

Bạn có thể tiếp tục phần này để được giải thích về cách giải quyết thách thức này.

  1. Dành một phút để xem lại mã giải pháp.

    Bạn có thể bắt đầu phá vỡ giải pháp và giải quyết đầu tiên để viết lời chào và mở đoạn văn đến thiết bị đầu cuối. Mã theo sau sẽ giải quyết việc hiển thị lời chào bằng Dear Ms. Barros, cách sử dụng nội suy chuỗi. Bây giờ bạn nên làm quen với mẫu hình Console.WriteLine($"Your text {yourVariable}");:

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

    Đầu ra mã là:

    Dear Ms. Barros,   
    

    Xem lại giải pháp đầy đủ mẫu. Nửa đầu của giải pháp sử dụng phép nội suy chuỗi để hiển thị từng phần của đoạn văn đầu tiên.

    Ghi

    Định dạng tổng hợp như là Console.WriteLine("Dear {0},", customerName) một giải pháp khả thi khác.

  2. Phần thứ hai của giải pháp hiển thị bảng so sánh bằng cách xây dựng một chuỗi dài theo bước sử dụng ghép nối chuỗi, với định dạng tổng hợp, string.Format() mã xác định định dạng (phần trăm và tiền tệ) PadRight()và .

    Mã sau đây xây dựng dòng đầu tiên của bảng với các bổ sung sau Console.WriteLine() mỗi bước xây dựng chuỗi 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);
    

    Đầu ra mẫu sau đây cho thấy cách xây dựng dòng đầu tiên của bảng so sánh trong ba bước.

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

Nếu bạn gặp khó khăn khi hoàn thành thử thách này, có thể bạn nên xem lại các đơn vị trước đó trước khi tiếp tục.