找不到符号,在参数中声明了数组

时间:2018-12-03 02:05:54

标签: java

此方法采用一组行星,并计算它们在所调用行星上的净x力分量。我在为a1编译时遇到未找到符号错误

public double calcForceExertedByX(Planet[] a1){
    double forceX;
    for (int i = 0; i < a1.length(); i++){
      forceX = forceX + 6.667e-11 *(mass*a1[i].mass)/( xxPos - a1[i].xxPos)*(xxPos - a1[i].xxPos);
    }
    return forceX;
}
下面的

是完整的代码,根据要求。

public class Planet {
  public double xxPos;
  public double yyPos;
  public double xxVel;
  public double yyVel;
  public double mass;
  public String imgFileName;
    public Planet(double xPs, double yPs, double xVl, double
    yVl, double m, String imge){
    xxPos = xPs;
    yyPos = yPs;
    xxVel = xVl;
    yyVel = yVl;
    mass = m;
    imgFileName = imge;

  }
  public Planet(Planet p){
    xxPos = p.xxPos;
    yyPos = p.yyPos;
    xxVel = p.xxVel;
    yyVel = p.yyVel;
    mass = p.mass;
    imgFileName = p.imgFileName;
  }
  public double calcDistance(Planet p1){
    double distx = xxPos - p1.xxPos;
    double disty = yyPos - p1.yyPos;
    double dist = Math.sqrt(distx*distx + disty*disty);
    return dist;
  }
  public double calcForceExertedBy(Planet p1){
    double force = 6.667e-11 *(mass*p1.mass)/(this.calcDistance(p1)*this.calcDistance(p1));
    return force;
  }
  public double calcForceExertedByX(Planet[] a1){
    double forceX;
    for (int i = 0; i < a1.length(); i++){
      forceX = forceX + 6.667e-11 *(mass*a1[i].mass)/( xxPos - a1[i].xxPos)*(xxPos - a1[i].xxPos);
    }
    return forceX;
  }
  public double calcForceExertedByY(Planet[] a1){
    double forceY;
    for (int i = 0; i <a1.length(); i++){
      forceY = forceY + 6.667e-11 *(mass*a1[i].mass)/( yyPos - a1[i].yyPos)*(yyPos - a1[i].yyPos);
    }
    return forceY;
  }
}

Windows 7 JAVAc 11.01

1 个答案:

答案 0 :(得分:2)

我猜正在尝试引用的错误是Planet[] a1没有方法length()。数组使用.length而不是方法调用。

forceX也未初始化。