문자열 보간에 대한 솔루션 검토

완료됨

다음 코드는 이전 단원의 과제에 사용 가능한 해결 방법 중 하나입니다.

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()를 사용하여 긴 문자열을 단계별로 작성함으로써 비교 테이블을 표시합니다.

    다음 코드는 문자열 comparisonMessage를 빌드하는 각 단계 후에 Console.WriteLine()을 추가하여 테이블의 첫 번째 줄을 빌드합니다.

    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
    

이 과제를 완료하는 데 문제가 있는 경우 계속 진행하기 전에 이전 단원을 복습해야 할 수도 있습니다.