特定の数値型チャレンジとして出力数学演算のソリューションを確認する
次のコードは、前のユニットの課題に対して考えられる解決策の 1 つです。
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}");
このコードは、単に "1 つの解決策" にすぎません。これは、この課題を解決するいくつかの方法がある可能性があるためです。 このソリューションでは、多くのキャスト (および変換の呼び出し) が使用されます。ただし、別のアプローチも同様に機能する可能性があります。 結果が次の出力と一致することを確認してください。
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
成功した場合は、おめでとうございます。 次のユニットの知識チェックに進みます。
大事な
この課題を完了できない場合は、先に進む前に前のユニットを確認する必要があります。