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;
}
}
视窗 7 JAVAc 11.01
解决方案
我猜想被引用的错误是Planet[] a1
没有方法length()
。数组使用.length
而不是方法调用。
也forceX
没有初始化。
推荐阅读
- javascript - 给定元素的特定索引,如何替换数组中的元素?
- android - Kotlin:如何将子数组减少为单个数组?
- reactjs - 根据表格内容动态设置列宽
- safari - 如何为 302 重定向的 url 启用 Safari 浏览器缓存
- java - Java 到 Arduino 的通信不同步 - 跳跃运动
- python - NameError: [class] 未定义
- c - 如何返回字符指针?
- python - pandas - DataError 没有要聚合的数字类型
- java - 需要触发 JavaFX TableView 的内置 Table Menu Button
- r - R 代码和 nycflights13 包中的数据探索分析