סקירת פתרון לאינטרפולציה של מחרוזות

הושלם

הקוד הבא הוא פתרון אפשרי אחד לאתגר ביחידה הקודמת.

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
    

אם אתה נתקל בבעיות בהשלמת אתגר זה, ייתכן שעליך לסקור את היחידות הקודמות לפני שתמשיך.