【问题标题】:Error when getting mouse move coords and dividing them by 32 (Math.Floor)获取鼠标移动坐标并将它们除以 32 时出错(Math.Floor)
【发布时间】:2017-06-14 19:23:19
【问题描述】:
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    //Create Graphics
    Graphics g = this.CreateGraphics();

    mx = Math.Floor(e.X / 32);
    my = Math.Floor(e.Y / 32);
}

mxmy 中的Floor 下出现错误(mx 和my 都是整数)

错误:严重性代码描述项目文件行抑制状态 错误 CS0121 调用在以下方法或属性之间不明确:“Math.Floor(decimal)”和 'Math.Floor(double)' GameAttempt3 c:\users\levyjrdesktop\documents\visual 工作室 2015\Projects\GameAttempt3\GameAttempt3\Form1.cs 95 活动

【问题讨论】:

  • 将错误添加到问题中...

标签: c# .net


【解决方案1】:

Floor 方法有两个重载(Double,Decimal):

public static decimal Floor(decimal d);
public static double Floor(double d);

所以你需要投射:

mx = Math.Floor((double)e.X / 32);
my = Math.Floor((double)e.Y / 32);

【讨论】:

  • 或者可选地使其 32.0 使用双重重载并放弃演员表。
【解决方案2】:

其实不用调用Math.Floor(..)e.Xe.Y已经是整数了,两个整数相除,会产生整数部分。

所以你可以简单地写:

mx = e.X / 32;
my = e.Y / 32;

或者如果你想使用Math.Floor(因为整数除法与Floor除法有点不同,你可以除以32.0d使其成为双倍:

mx = (int) Math.Floor(e.X / 32.0d);
my = (int) Math.Floor(e.Y / 32.0d);

因此,您必须再次转换为 int

【讨论】:

  • @KHF445:你的意思是没用。请提供一些信息。
  • 在使用 .0 或 .0d 时出现另一个错误 严重性代码 描述 项目文件行抑制状态错误 CS0266 无法将类型“double”隐式转换为“int”。存在显式转换(您是否缺少演员表?) GameAttempt3 c:\users\levyjrdesktop\documents\visual studio 2015\Projects\GameAttempt3\GameAttempt3\Form1.cs 97 Active
  • @KHF445:应该修复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-03
相关资源
最近更新 更多