Dize ilişkilendirme çözümünü gözden geçirme
Aşağıdaki kod, önceki ünitede yer alan sınamanın olası çözümlerinden biridir.
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);
Bu kod yalnızca "olası bir çözüm" çünkü mantığı nasıl uyguladığınıza çok bağlı. Dizeleri, tuş takımı dizelerini vb. biçimlendirmek için bu modülde ele alınan teknikleri kullandıysanız ve çıktı sınama çıktısı ile eşleşiyorsa, harika bir iş çıkarmış olursunuz!
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
Başarılı olduysanız tebrikler!
Çözüm ayrıntıları
Verilen çözümün bu sınamayı nasıl çözdüğüne ilişkin bir açıklama için bu bölüme devam edebilirsiniz.
Çözüm kodunu gözden geçirmek için bir dakika bekleyin.
Çözümü parçalara ayırarak işe başlayabilir ve önce selamlamayı yazıp terminalde açılış paragrafını oluşturarak çözebilirsiniz. Aşağıdaki kod, dize ilişkilendirmesini kullanarak selamlamayı
Dear Ms. Barros,görüntülemeyi çözer. Artık deseniConsole.WriteLine($"Your text {yourVariable}");hakkında bilgi sahibi olmanız gerekir:string customerName = "Ms. Barros"; Console.WriteLine($"Dear {customerName},");Kod çıkışı:
Dear Ms. Barros,Örnek tam çözümü yeniden gözden geçirin. Çözümün ilk yarısı, ilk paragrafın her bir bölümünü görüntülemek için dize ilişkilendirmesini kullanır.
Uyarı
Gibi
Console.WriteLine("Dear {0},", customerName)bileşik biçimlendirme başka bir olası çözümdür.Çözümün ikinci bölümü, bileşik biçimlendirme,
string.Format()biçim tanımlayıcıları (yüzde ve para birimi) vePadRight()ile dize birleştirmeyi kullanarak adım adım uzun bir dize oluşturarak karşılaştırma tablosunu görüntüler.Aşağıdaki kod, tablonun ilk satırını, dizesi
Console.WriteLine()oluşturulurken her adımından sonracomparisonMessageeklenerek oluşturur.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);Aşağıdaki örnek çıktı, karşılaştırma tablosunun ilk satırının üç adımda nasıl derlenmiş olduğunu gösterir.
Magic Yield Magic Yield 12.75% Magic Yield 12.75% $55,000,000.00
Bu sınamaya tamamlama konusunda sorun yaşadıysanız, devam etmeden önce önceki üniteleri gözden geçirmeniz iyi olabilir.