Java 数组未正确使用对象更新

Java 数组未正确使用对象更新,java,Java,因此,我的SolarSystem类中的planetArray对象数组没有使用指定的对象进行更新。真的只是想知道我做这件事的方式有什么问题。这是一个课程,所以我不想找人完全重写我的工作,只是一些具体问题的指针 以下是我们讲师使用的自动测试: /*This is the automatic test class for CS-110 coursework 2. The output of the student's program * under test should match the str

因此,我的SolarSystem类中的planetArray对象数组没有使用指定的对象进行更新。真的只是想知道我做这件事的方式有什么问题。这是一个课程,所以我不想找人完全重写我的工作,只是一些具体问题的指针

以下是我们讲师使用的自动测试:

/*This is the automatic test class for CS-110 coursework 2. The output of the student's program
 * under test should match the string TARGET_OUTPUT
 */
public class AutoTest {

    static final String TARGET_OUTPUT =
            "Our System\n"
            + "Planet Mercury has a mass of 0.055 Earths, is 0.387AU from its star, and orbits in 88.025 days\n"
            + "Planet Venus has a mass of 0.815 Earths, is 0.723AU from its star, and orbits in 224.629 days\n"
            + "Planet Earth has a mass of 1.0 Earths, is 1.0AU from its star, and orbits in 365.25 days\n"
            + "Planet Mars has a mass of 0.107 Earths, is 1.52AU from its star, and orbits in 1.874 years\n"
            + "Planet Jupiter has a mass of 317.8 Earths, is 5.2AU from its star, and orbits in 11.858 years\n"
            + "Planet Saturn has a mass of 95.2 Earths, is 9.58AU from its star, and orbits in 29.652 years\n"
            + "Planet Uranus has a mass of 14.5 Earths, is 19.2AU from its star, and orbits in 84.13 years\n"
            + "Planet Neptune has a mass of 17.1 Earths, is 30.05AU from its star, and orbits in 164.728 years\n";

    public static void main(String[] args) {
        SolarSystem ourSystem = new SolarSystem("Our System");

        //Add planets in our solar system
        ourSystem.addPlanet("Mercury", 0.0553, 0.387);
        ourSystem.addPlanet("Venus", 0.815, 0.723);
        ourSystem.addPlanet("Earth", 1.0, 1.0);
        ourSystem.addPlanet("Mars", 0.107, 1.52);
        ourSystem.addPlanet("Jupiter", 317.8, 5.20);
        ourSystem.addPlanet("Saturn", 95.2, 9.58);
        ourSystem.addPlanet("Uranus", 14.5, 19.20);
        ourSystem.addPlanet("Neptune", 17.1, 30.05);

        //Check the output
        if(ourSystem.toString().equals(TARGET_OUTPUT)){
            System.out.println("Pass!");
        } else {
            System.out.println("Fail!\n*****");
            System.out.println("Expected output:\n");
            System.out.println(TARGET_OUTPUT);
            System.out.println("\n\nActual output:\n");
            System.out.println(ourSystem);
        }

    }
}
这是我的SolarSystem课程:

public class SolarSystem{
    public static final int PLANET_MAX = 10;
    public static int planetCount = 0;
    String systemName;
    Planet[] planetArray = new Planet[PLANET_MAX];
    public SolarSystem(String name){
        systemName = name;
    }

    public void addPlanet(String planetName, double planetMass, double planetDistance){
        Planet newPlanet = new Planet(planetName, planetMass, planetDistance);
        newPlanet.calculatePeriod(planetDistance);
        planetArray[planetCount] = newPlanet;
        planetCount++;
    }

    public String toString(){
        String myString = systemName + "\n";
        for(int i = 0; i <= PLANET_MAX; i++){
            int count = 0;
            String name = planetArray[count].getPlanetName();
            double mass = planetArray[count].getPlanetMass();
            double distance = planetArray[count].getPlanetDistance();
            double period = planetArray[count].getPlanetPeriod();
            if(period < 1){
                myString = myString + "Planet " + name + " has a mass of " + mass + " Earths, is " + distance + " from its star and orbits in " + period + " days.\n";
            }
            else{
                myString = myString + "Planet" + name + "has a mass of" + mass + "Earths, is" + distance + "from its star and orbits in" + period + "years.\n";
            }
            count++;
        }
        return myString;
    }
}

我的输出显示了填充了mecury值的数组,它只显示mercury,因为它保持为0,第一个索引。此外,在for循环中,它应该小于
PLANET_MAX
,因为这是元素的数量(长度)。否则您将获得
java.lang.arrayindexoutofboundsexception
。像这样做:

public String toString(){
        String myString = systemName + "\n";
        for(int i = 0; i < PLANET_MAX; i++){ //< instead of <=
            String name = planetArray[i].getPlanetName();
            double mass = planetArray[i].getPlanetMass();
            double distance = planetArray[i].getPlanetDistance();
            double period = planetArray[i].getPlanetPeriod();
公共字符串toString(){
字符串myString=systemName+“\n”;

for(int i=0;i
但是count总是0,因为前一行
int count=0;
。你应该使用循环变量
i
。顺便说一句,不客气。我注意到我每次初始化循环中的count都犯了愚蠢的错误。现在我接受了你的改进,我又卡住了。我正在抛出一些nullpointerexcep我看不出它是空的,因为我可以看到它被初始化了。如果我手动输入一个索引而不是我,我会得到索引行星垃圾邮件。问题不在索引
I
中,你没有相应地填充数组。我仍然不明白你的意思,当我手动将0、1、2等替换为I时,我得到了正确的结果output@LukeWheeldon欢迎来到SO!因为你(相对)是新来的,你可能想检查一下这个
public String toString(){
        String myString = systemName + "\n";
        for(int i = 0; i < PLANET_MAX; i++){ //< instead of <=
            String name = planetArray[i].getPlanetName();
            double mass = planetArray[i].getPlanetMass();
            double distance = planetArray[i].getPlanetDistance();
            double period = planetArray[i].getPlanetPeriod();