【问题标题】:Cannot invoke mult(float) on the primitive type float无法在原始类型 float 上调用 mult(float)
【发布时间】:2018-09-21 16:30:18
【问题描述】:

我正在处理一个简单的重力程序。我的程序采用粒子并根据重力公式将它们相互吸引。不幸的是,一旦我尝试将力与PVector.mult() 相乘,我就会在标题中看到错误:

无法在基本类型 float 上调用 mult(float)。

这是我的方法代码。 G 在别处定义。

public float distance(Particle other) {
    return location.sub(other.location).mag();
}

public PVector direction(Particle other) {
    return location.sub(other.location).normalize();
}

public void gravity(Particle other) {
    float grav = (G*((mass * other.mass)/pow(distance(other), 2)));
    if(distance(other) != 0) {
    acceleration.add(distance(other).mult(grav));
}

为什么我不能通过一个浮点数到期的浮点数?

【问题讨论】:

标签: parameter-passing processing


【解决方案1】:

让我们把这条线拆开,分成多个步骤:

acceleration.add(distance(other).mult(grav));

这是我尝试将其分成多行的尝试:

float grav = 42;
float distanceFromOther = distance(other);
float multipliedValue = distanceFromOther.mult(grav);
acceleration.add(multipliedValue);

希望这能让发生的事情更加明显:您试图在 float 值上调用 mult(),但这是行不通的。您需要在 PVector 或另一个包含 mult() 函数的类上调用 mult

【讨论】:

  • 多么简单的错误。我混淆了距离和方向,可能是因为它们都以 d 开头。好尴尬~~
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-11
  • 2018-11-03
  • 2012-04-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多