مراجعة حل لاستنتاج السلسلة
تُعد التعليمات البرمجية التالية أحد الحلول الممكنة للتحدي من الوحدة السابقة.
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
نهنئك في حال نجاحك!
تفاصيل الحل
يمكنك متابعة هذا القسم للحصول على شرح حول كيفية حل الحل المحدد لهذا التحدي.
خذ دقيقة لمراجعة رمز الحل.
يمكنك البدء في تقسيم الحل وحل أولا لكتابة فقرة الترحيب وفتحها إلى المحطة الطرفية. التعليمات البرمجية التالية تحل عرض الترحيب
Dear Ms. Barros,باستخدام استنتاج السلسلة. يجب أن تكون على دراية الآن بالنمطConsole.WriteLine($"Your text {yourVariable}");:string customerName = "Ms. Barros"; Console.WriteLine($"Dear {customerName},");إخراج التعليمات البرمجية هو:
Dear Ms. Barros,راجع نموذج الحل الكامل مرة أخرى. يستخدم النصف الأول من الحل استنتاج السلسلة لعرض كل جزء من الفقرة الأولى.
إشعار
التنسيق المركب مثل
Console.WriteLine("Dear {0},", customerName)هو حل آخر ممكن.يعرض الجزء الثاني من الحل جدول المقارنة عن طريق إنشاء سلسلة طويلة خطوة بخطوة باستخدام تسلسل السلسلة،
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
إذا واجهت مشكلة في إكمال هذا التحدي، فربما يجب عليك مراجعة الوحدات السابقة قبل المتابعة.