ตรวจทานโซลูชันเพื่อประมาณค่าจากข้อมูลในสตริง

เสร็จสมบูรณ์เมื่อ

โค้ดต่อไปนี้เป็นหนึ่งในวิธีแก้ไขปัญหาที่เป็นไปได้สําหรับการทดสอบจากหน่วยก่อนหน้า

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

    โค้ดต่อไปนี้สร้างบรรทัดแรกของตารางพร้อมส่วน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
    

หากคุณประสบปัญหาในการดําเนินการการทดสอบนี้ให้เสร็จสมบูรณ์ บางทีคุณควรตรวจสอบหน่วยก่อนหน้านี้ก่อนที่คุณจะดําเนินการต่อ