연습 - 수학 연산을 특정 숫자 형식으로 출력하는 챌린지 완료

완료됨

다음은 코딩 문제를 해결하기 위해 캐스팅 및 변환에 대해 배운 내용을 사용할 수 있는 두 번째 기회입니다.

다음 과제는 축소 및 확대 변환의 영향을 고려하여 캐스팅 값의 의미를 이해하는 데 도움이 됩니다.

  1. 이전 연습에서 모든 코드를 삭제하거나 주석 처리

  2. 다음 "starter" 코드를 입력합니다.

    int value1 = 11;
    decimal value2 = 6.2m;
    float value3 = 4.3f;
    
    // Your code here to set result1
    // Hint: You need to round the result to nearest integer (don't just truncate)
    Console.WriteLine($"Divide value1 by value2, display the result as an int: {result1}");
    
    // Your code here to set result2
    Console.WriteLine($"Divide value2 by value3, display the result as a decimal: {result2}");
    
    // Your code here to set result3
    Console.WriteLine($"Divide value3 by value1, display the result as a float: {result3}");
    
  3. 시작 코드의 코드 주석을 사용자 고유의 코드로 바꿔 문제를 해결합니다.

    • result1해결: value1value2나눕니다. 결과를 int
    • result2해결: value2value3나누기, 결과를 decimal
    • result3해결: value3value1나누기, 결과를 float

    출력이 다음과 유사하게 문제를 해결합니다.

    Divide value1 by value2, display the result as an int: 2
    Divide value2 by value3, display the result as a decimal: 1.4418604651162790697674418605
    Divide value3 by value1, display the result as a float: 0.3909091
    
  4. Visual Studio Code 파일 메뉴에서 저장을 선택합니다.

    코드를 작성하거나 실행하기 전에 Program.cs 파일을 저장해야 합니다.

  5. 탐색기 패널에서 TestProject 폴더 위치에서 터미널을 열려면 TestProject를 마우스 오른쪽 단추로 클릭한 다음 통합 터미널에서 열기를 선택합니다.

    터미널 패널이 열리고 터미널이 TestProject 폴더 위치에 열려 있음을 보여 주는 명령 프롬프트가 포함되어야 합니다.

  6. 터미널 명령 프롬프트에서 코드를 실행하려면 dotnet run 을 입력한 다음 Enter 키를 누릅니다.

    다음과 같은 출력이 표시됩니다.

    Divide value1 by value2, display the result as an int: 2
    Divide value2 by value3, display the result as a decimal: 1.4418604651162790697674418605
    Divide value3 by value1, display the result as a float: 0.3909091
    

    메모

    "실행할 프로젝트를 찾을 수 없습니다."라는 메시지가 표시되면 터미널 명령 프롬프트에 예상 TestProject 폴더 위치가 표시되는지 확인합니다. 예: C:\Users\someuser\Desktop\csharpprojects\TestProject>

문제가 발생하여 솔루션을 피킹해야 하는지, 아니면 성공적으로 완료해야 하는지에 관계없이 이 문제에 대한 해결 방법을 계속 확인하세요.