특정 숫자 유형 챌린지에 대한 수학 연산 출력 솔루션 검토

완료됨

다음 코드는 이전 단원의 챌린지에 대한 한 가지 가능한 솔루션입니다.

int value1 = 11;
decimal value2 = 6.2m;
float value3 = 4.3f;

// The Convert class is best for converting the fractional decimal numbers into whole integer numbers
// Convert.ToInt32() rounds up the way you would expect.
int result1 = Convert.ToInt32(value1 / value2);
Console.WriteLine($"Divide value1 by value2, display the result as an int: {result1}");

decimal result2 = value2 / (decimal)value3;
Console.WriteLine($"Divide value2 by value3, display the result as a decimal: {result2}");

float result3 = value3 / value1;
Console.WriteLine($"Divide value3 by value1, display the result as a float: {result3}");

이 코드는 여러 가지 방법으로 이 문제를 해결할 수 있기 때문에 "한 가지 가능한 솔루션"에 불과합니다. 솔루션은 많은 캐스팅(및 변환 호출)을 사용합니다. 그러나 다른 방법이 똑같이 잘 작동할 수 있습니다. 결과가 다음 출력과 일치하는지 확인합니다.

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

성공하면 축하합니다! 다음 단원에서 지식 점검을 계속 진행합니다.

중요하다

이 챌린지를 완료하는 데 문제가 있는 경우 계속하기 전에 이전 단원을 검토해야 할 수도 있습니다.