我是初学Java用户,在不同的类中使用两个公共数组时遇到错误。该程序应该通过数学积分来模拟3个星际体。注意:我是这个网站的新用户,但尚无法确定格式。
package gravitationalmodel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
public class GravitationalModel
{
private Object timer;
private GravitationalObject G1;
private GravitationalObject G2;
private GravitationalObject G3;
public static double[] xArray;
public static double[] yArray;
final public double gravConst = 0.000000000067;
final public int refreshRate = 25;
public GravitationalModel()
{
Timer timer = new Timer(refreshRate, (ActionListener) this); // refreshes model every 25 milliseconds
}
public static void main(String[] args)
{
GravitationalObject G1 = new GravitationalObject(500, 500, 10, 10, 10); // creates 3 interstellar objects taking x position,
GravitationalObject G2 = new GravitationalObject(400, 400, 10, 10, 10); // y position, x veleocity component, y velocity component
GravitationalObject G3 = new GravitationalObject(600, 600, 10, 10, 10); // mass of the object
xArray = new double[1920]; // assigns all elements of array to 0
for (int x = 0; x < 1920; ++x)
{
xArray[x] = 0;
//System.out.println(xArray[x]);
}
yArray = new double[1200];
for (int x = 0; x < 1200; ++x)
{
yArray[x] = 0;
//System.out.println(yArray[x]);
}
}
public void actionPerformed(ActionEvent e) // refreshes model
{
G1.refreshVectorField();
G2.refreshVectorField();
G3.refreshVectorField();
G1.refreshObjects();
G2.refreshObjects();
G3.refreshObjects();
}
}
package gravitationalmodel;
public class GravitationalObject
{
private double xPos; //location and speed of gravitational object
private double yPos;
private double vX;
private double vY;
private double mass;
public GravitationalObject(double x, double y, double xv, double yv, double m)
{
xPos = x;
yPos = y;
vX = xv;
vY = yv;
mass = m;
}
public void refreshVectorField() // calculates gravitational field
{
int k = 1;
for (int x = 0; x < 1920; ++x)
{
if (xPos > x)
k = 1;
else
k= -1;
if (xPos - x == 0)
++x;
xArray[x] += k * (gravConst * mass) / Math.sqrt(xPos - x, 2.0); // errors on this line
}
for (int y = 0; y < 1200; ++y)
{
if (yPos < y)
k = 1;
else
k= -1;
yArray[y] += (gravConst * mass) / Math.sqrt(Math.abs(yPos - y), 2); //errors on this line
}
}
public void refreshObjects()
{
vX += refreshRate/1000 * xArray[xPos]; // errors on these lines
vY += refreshRate/1000 * yArray[yPos];
xPos += refreshRate/1000 * vX;
xPos += refreshRate/1000 * vX;
}
}
答案 0 :(得分:0)
您试图从GravitationalModel.xArray
访问GravitationalObject
而未完全限定静态字段。要么完全限定字段,要么在GravitationalModel
中创建一个方法来完成工作。
请注意,Java中的公共可变字段通常不受欢迎,因为它会破坏封装。最好让对象通过方法封装数据和行为,并将变量保密。
答案 1 :(得分:0)
作为初学者,你不应该快点。很快你就能写出很酷的节目了。这个程序中有很多错误,甚至程序也没有任何逻辑。你应该先练习一些小程序来获得基础知识。就像你创建了以下根本不使用的对象一样。
GravitationalObject G1 = new GravitationalObject(500, 500, 10, 10, 10); // creates 3 interstellar objects taking x position,
GravitationalObject G2 = new GravitationalObject(400, 400, 10, 10, 10); // y position, x veleocity component, y velocity component
GravitationalObject G3 = new GravitationalObject(600, 600, 10, 10, 10); // mass of the object
现在出现了程序错误。
如果要访问其他类中的实例变量gravConst
或refreshRate
,则需要创建类的对象并访问它们。
或者你可以创建它们静态并将它们导入到像这样的类
import static gravitationalmodel.GravitationalModel.xArray;
import static gravitationalmodel.GravitationalModel.yArray;
import static gravitationalmodel.GravitationalModel.gravConst;
import static gravitationalmodel.GravitationalModel.refreshRate;
Math.sqrt(double)
不会带2个参数,因此我认为您要使用xArray[x] += k * (gravConst * mass) / Math.pow(xPos - x, 2.0);
并将double转换为int作为数组索引只能是整数,如下所示
vX += refreshRate / 1000 * xArray[(int)xPos];
vY += refreshRate / 1000 * yArray[(int)yPos];
所以你的最终节目如下:
package gravitationalmodel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
public class GravitationalModel {
private Object timer;
private GravitationalObject G1;
private GravitationalObject G2;
private GravitationalObject G3;
public static double[] xArray;
public static double[] yArray;
final static public double gravConst = 0.000000000067;
final static public int refreshRate = 25;
public GravitationalModel() {
Timer timer = new Timer(refreshRate, (ActionListener) this); // refreshes
// model
// every
// 25
// milliseconds
}
public static void main(String[] args) {
GravitationalObject G1 = new GravitationalObject(500, 500, 10, 10, 10); // creates
// 3
// interstellar
// objects
// taking
// x
// position,
GravitationalObject G2 = new GravitationalObject(400, 400, 10, 10, 10); // y
// position,
// x
// veleocity
// component,
// y
// velocity
// component
GravitationalObject G3 = new GravitationalObject(600, 600, 10, 10, 10); // mass
// of
// the
// object
xArray = new double[1920]; // assigns all elements of array to 0
for (int x = 0; x < 1920; ++x) {
xArray[x] = 0;
// System.out.println(xArray[x]);
}
yArray = new double[1200];
for (int x = 0; x < 1200; ++x) {
yArray[x] = 0;
// System.out.println(yArray[x]);
}
}
public void actionPerformed(ActionEvent e) // refreshes model
{
G1.refreshVectorField();
G2.refreshVectorField();
G3.refreshVectorField();
G1.refreshObjects();
G2.refreshObjects();
G3.refreshObjects();
}
}
和第二课
package gravitationalmodel;
import static gravitationalmodel.GravitationalModel.xArray;
import static gravitationalmodel.GravitationalModel.yArray;
import static gravitationalmodel.GravitationalModel.gravConst;
import static gravitationalmodel.GravitationalModel.refreshRate;
public class GravitationalObject {
private double xPos; // location and speed of gravitational object
private double yPos;
private double vX;
private double vY;
private double mass;
public GravitationalObject(double x, double y, double xv, double yv,
double m) {
xPos = x;
yPos = y;
vX = xv;
vY = yv;
mass = m;
}
public void refreshVectorField() // calculates gravitational field
{
int k = 1;
for (int x = 0; x < 1920; ++x) {
if (xPos > x)
k = 1;
else
k = -1;
if (xPos - x == 0)
++x;
xArray[x] += k * (gravConst * mass) / Math.pow(xPos - x, 2.0); // errors
// on
// this
// line
}
for (int y = 0; y < 1200; ++y) {
if (yPos < y)
k = 1;
else
k = -1;
yArray[y] += (gravConst * mass) / Math.pow(Math.abs(yPos - y), 2); // errors
// on
// this
// line
}
}
public void refreshObjects() {
vX += refreshRate / 1000 * xArray[(int)xPos]; // errors on these lines
vY += refreshRate / 1000 * yArray[(int)yPos];
xPos += refreshRate / 1000 * vX;
xPos += refreshRate / 1000 * vX;
}
}