首页 > 解决方案 > 未找到符号,在参数中声明数组

问题描述

该方法采用一系列行星并计算它们在所调用行星上的净 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;
  }
}

视窗 7 JAVAc 11.01

标签: java

解决方案


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

forceX没有初始化。


推荐阅读