查看作为特定数字类型质询的输出数学运算的解决方案
以下代码是上一单元中质询的一种可能解决方案。
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
如果你成功了,恭喜! 继续学习下一单元中的知识检查。
重要
如果在完成此挑战时遇到问题,也许你应该先查看以前的单元,然后再继续作。