C# 为什么是数学。圆不';我不总是打成双

C# 为什么是数学。圆不';我不总是打成双,c#,math,double,decimal,C#,Math,Double,Decimal,问题在于double-ORB和double-FT 由于某种原因,我不能对它们使用Math.Round。它说: 以下方法或属性之间的调用不明确: “数学四舍五入(双)”和“数学四舍五入(十进制)” 我只是不明白为什么前两个有用,但后两个不行 您正在使用int值调用Math.Round。您可能想先将它们转换为double:Math.Round(1.0*罚球…) 没有Math。舍入(int)overload,但是double和decimal都有重载,并且int可以很好地转换为这两种重载。因此,调用将是

问题在于
double-ORB
double-FT

由于某种原因,我不能对它们使用
Math.Round
。它说:

以下方法或属性之间的调用不明确: “数学四舍五入(双)”和“数学四舍五入(十进制)”


我只是不明白为什么前两个有用,但后两个不行

您正在使用
int
值调用
Math.Round
。您可能想先将它们转换为
double
Math.Round(1.0*罚球…


没有
Math。舍入(int)
overload,但是
double
decimal
都有重载,并且
int
可以很好地转换为这两种重载。因此,调用将是不明确的。

如果您尝试划分整数,结果将是整数。所以,你不能把这个数字四舍五入。在除法和舍入之前将其转换为双精度:

int fieldGoals =                int.Parse(Console.ReadLine());
int fieldGoalAttempts =         int.Parse(Console.ReadLine());
int threePointFieldGoals =      int.Parse(Console.ReadLine());
int turnovers =                 int.Parse(Console.ReadLine());
int offensiveRebounds =         int.Parse(Console.ReadLine());
int opponentDefensiveRebounds = int.Parse(Console.ReadLine());
int freeThrows =                int.Parse(Console.ReadLine());
int freeThrowAttempts =         int.Parse(Console.ReadLine());

double eFG = Math.Round( (fieldGoals + 0.5 * threePointFieldGoals) / fieldGoalAttempts );
double TOV = Math.Round( turnovers / (fieldGoalAttempts + 0.44 * freeThrowAttempts + turnovers) );
double ORB = Math.Round( offensiveRebounds / (offensiveRebounds + opponentDefensiveRebounds) );
double FT  = Math.Round( freeThrows / fieldGoalAttempts );

在前两个调用中,您都添加了一些内容
0.5
0.44
都将值转换为双倍值,因为
0.5
0.44
都被视为双倍值。但是当你使用后两个时,它们都只使用整数,既不是双精度的,也不是十进制的,并且可以转换成任意一个。要解决这个问题,你只需要做
Math.Round((double)(*计算*)

或者,事实上更好的方法是将其中一个值转换为double-这样,它将以double计算除法。
(双重)进攻方/(进攻方+对方防守方)


(double)罚球/fieldgoalattests

在前两种情况下,您已将参数隐式转换为数学。使用double(即0.5和0.44)作为乘法因子,四舍五入为double


在后两项中,您没有。

非常感谢。现在我明白为什么前两个有效而其他的无效了。再次感谢您。@IvailoKorakov很高兴能帮助您!将所有
int
变量声明为
double
,这似乎是您的最佳选择。它们都只使用整数,既不是双精度的,也不是十进制的,并且可以转换为任意一个-这是一个重要的点。您准确地解释了发生的情况:
Math.Round()
没有整数参数的实现,但是有一个
double
和一个
decimal
重载。这就是为什么传递整型参数时会出现歧义。
double ORB = Math.Round( (double)offensiveRebounds / (offensiveRebounds + opponentDefensiveRebounds) );
double FT  = Math.Round( (double)freeThrows / fieldGoalAttempts );