我正在研究流体力学问题,我决定使用 Java 而不是 excel 来解决问题,因为我只是想要经验。不过在过去的 4 个小时里我一直被困在这一部分上。我有一个在一个类中创建的数组,我需要将所有数据传递到另一个类,以便我可以将其绘制成图表。此时我真的只是在寻找答案。我现在脑子很乱,只想把这件事完成。
我发现其他人也有类似的问题,但我无法理解他们给出的解决方案,现在我的大脑很困惑,这就是为什么我更希望有人能够准确地告诉我要输入什么内容,但如有任何帮助,我们将不胜感激。
public class Eight_c {
public static void main(String [] args) {
Eight_c ball = new Eight_c();
...
int count = 0;
double[] y = new double[101];
double[] x = new double[101];
// Calculates the Distance the ball has traveled using increments of .1s
for(double t = 0; t<=time; t=t+.1) {
y[count] = t;
x[count] = d;
V1 = ball.Velocity(a, V2, dt);
Fd = ball.Drag_Force(V2);
a = ball.Acceleration(Fd, m);
d = ball.Distance(V2, a, dt) + d;
...
上面是我创建两个数组 x 和 y 的位置。
下面是他们需要去的地方。
public class Graph {
public static void main(String [] args) {
double [] x;
double [] y;
// create your PlotPanel (you can use it as a JPanel)
Plot2DPanel plot = new Plot2DPanel();
// add a line plot to the PlotPanel
plot.addLinePlot("Distance vs Time", x, y);
// put the PlotPanel in a JFrame, as a JPanel
JFrame frame = new JFrame("a plot panel");
frame.setContentPane(plot);
frame.setVisible(true);
}
}
最佳答案
main()
是程序的入口点。您不能有 2 个入口点。因此,您需要第一个类的 main 方法来调用第二个类的方法,并为其提供 x 和 y 作为参数:
public class Graph {
public void render(double[] x, double[] y) {
// create your PlotPanel (you can use it as a JPanel)
Plot2DPanel plot = new Plot2DPanel();
// add a line plot to the PlotPanel
plot.addLinePlot("Distance vs Time", x, y);
// put the PlotPanel in a JFrame, as a JPanel
JFrame frame = new JFrame("a plot panel");
frame.setContentPane(plot);
frame.setVisible(true);
}
}
在Eight_c的main方法中:
// create a Graph object:
Graph graph = new Graph();
// ask it to render x and y:
graph.render(x, y);
如果您不知道什么是方法和对象,以及如何将参数传递给方法,我发现开始使用 Swing 会很奇怪。这有点像你还没学会走路就尝试驾驶空中客车。
阅读一本有关 Java 和一般编程的介绍性书籍。使用简单的基于控制台的程序进行锻炼和练习。然后才开始使用 Swing。
关于java - 需要将数组传递给另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25714981/