简体   繁体   English

Java中的ArrayList仅打印列表中的最后一个对象

[英]ArrayList in Java only prints the last object in the List

I'm taking an online Computer Science Course for school and I have hit a bump in the road that after two days I still can not figure out, I have turned to the community. 我正在上学校的在线计算机科学课程,遇到了一个颠簸,两天后我仍然不明白,我转向了社区。

I have written a piece of code that I need to print the catapult distances. 我已经编写了一段代码,需要打印弹射器的距离。 I have put these objects in an ArrayList and have developed a class file for the catapult instance. 我将这些对象放在ArrayList中,并为弹射器实例开发了一个类文件。 I want my code to print a list of the distances and the MPH at the start of the row (You can see this with the source files below with an example of what I want it to look like). 我希望我的代码在行的开头打印距离和MPH的列表(您可以在下面的源文件中看到此列表,并提供我想要的样子的示例)。 Here is the code I have developed up to this point: 这是我到目前为止开发的代码:

Catapult.java Catapult.java

public class Catapult {
    static double speed, angle, MPS, Rad, R;

Catapult(double launchSpeed, double launchAngle) {
    angle = launchAngle;
    speed = launchSpeed;

public void convMPHtoMPS() {
    MPS = speed * 0.44704;
}

public void convDegToRad() {
    Rad = Math.toRadians(angle);
}

public void calcDistance() {
    R = (Math.pow(MPS, 2)*Math.sin(2*Rad))/(Math.pow(9.8, 2));
}

public void convMtoFt() {
    R = R * 3.28084;
}

public double getMPH() {
    return speed;
}

public double getAngle() {
    return angle;
}

public double getMetersPerSecond() {
    return MPS;
}

public double getRadians() {
    return Rad;
}

public double getDistance() {
    return R;
}
}

CatapultTester.java CatapultTester.java

    public static void calcData() {
        int mph = 25;
        for (int i = 0; i < 7; i++) {
            int deg = 25;
            for (int j = 0; j < 6; j++) {
                CP.add(new Catapult(mph, deg));
                //System.out.println(mph);
                //System.out.println(deg);
                deg += 5;   
            }
            mph += 5;
        }
        System.out.println(CP.size());
        Catapult data;
        for (int index = 0; index < CP.size(); index++) {
            data = CP.get(index);
            data.convMPHtoMPS();
            data.convDegToRad();
            data.calcDistance();
            data.convMtoFt();
            //System.out.println(data.getMPH());
            //System.out.println(data.getDistance());
        }
        presentData();
    }
    public static void presentData() {
        Catapult data;
        for (int index = 0; index < CP.size(); index++) {
            data = CP.get(index);
            System.out.printf("%1f", data.getMPH());
            for (int j = 0; j < 6; j++) {
                System.out.printf("%4.2f", data.getDistance());
            }
        }
    }

Expected Output 预期产量

MPH    25 deg    30 deg    35 deg    40 deg     45 deg    50 deg
25     distance  distance  distance  distance   distance  distance
30     distance  distance  distance  distance   distance  distance
...

Distance is the formula declared in Catapult.java 距离是Catapult.java中声明的公式

R = (Math.pow(MPS, 2)*Math.sin(2*Rad))/Math.pow(9.8)/2));

Actual Output: 实际输出:

MPH    25 deg    30 deg    35 deg    40 deg     45 deg    50 deg
55     20.34     20.34     20.34     20.34      20.34     20.34
55     20.34     20.34     20.34     20.34      20.34     20.34
55     20.34     20.34     20.34     20.34      20.34     20.34
...

Thank you all for your help and I apologize for any grammatical errors or confusion with code. 谢谢大家的帮助,对于任何语法错误或代码混淆,我深表歉意。 I am new to the Java programming language! 我是Java编程语言的新手!

您不应该在您的情况下使用静态变量,因为它将在该类的所有对象(弹射器)中通用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM