修改方法,以便为它们指定月球的数组索引值,其值为name或radius

时间:2016-11-01 22:19:23

标签: processing

所以我的问题部分是''修改getMoonName()和getMoonRadius(),因此给它们一个月亮的数组索引值,其名称或半径是必需的。'' 我已经尝试添加卫星[i] .getRadius但最终得到''变量我不存在''。这是代码。 行星类

    public class Planet
    {
      private float angle=0.01;
      // add class member variables here
      private String name;
      private float radius;
      private float distance;
      private float speed;
      private Moon[] moons;


      // add constructor here
      public Planet(String n, float r, float d, float s, Moon[] m)
      {
        this.name=n;
        this.radius=r;
        this.distance=d;
        this.speed=s;
        this.moons=m;
      }

      // add other methods here
      public String getName()
      {
        return name;
      }

      public float getRadius()
      {
        return radius;
      }

      public float getDistance()
      {
        return distance;
      }

      public float getSpeed()
      {
        return speed;
      }

      public Moon[] getMoons()
      {
        return moons;
      }

      public void setRadius(float r)
      {
        this.radius=r;
      }


      public String getMoonName()
      {
        return moons[i].getName();
      }

      public float getMoonRadius()
      {
        return moons[i].getRadius();
      }

      public String toString()
      {
        int n=0;
        for  (int i=0; i<moons.length; i++)
        {
          n++;
        }
        return "Planet" + name + ("Radius: " +radius +"Distance: " +distance) +n +"moons.";
      } 

      public void printMoons()
      {
        for  (int i=0; i<moons.length; i++)
        {
          System.out.println(moons[i]);
        }
      }


      // This will display the moon when other code is completed.  You don't need to understand this code.
     public void display()
     {  
     angle=angle+(0.01*speed);
     pushMatrix();
      rotate(angle);
      translate(distance,0);
      fill(255, 255, 255);
      ellipse(0, 0, radius*2, radius*2);    

      for(Moon moon: getMoons())
        moon.display();

        popMatrix();    
      }
    }`

MOON CLASS

public class Moon
    {
      private float angle=0.01;
      // add class member variables here
      private String name;
      private float radius;
      private float distance;
      private float speed;
      private int orbitalPeriod;


      // add constructor here
      public Moon(String n, float r, float d, float s, int o)
      {
        this.name=n;
        this.radius=r;
        this.distance=d;
        this.speed=s;
        this.orbitalPeriod=o;
      }



      // add other methods here
      public String getName()
      {
        return name;
      }

      public float getRadius()
      {
        return radius;
      }

      public float getDistance()
      {
        return distance;
      }

      public float getSpeed()
      {
        return speed;
      }

      public float getOrbitalPeriod()
      {
        return orbitalPeriod;
      }

      public void setName(String n)
      {
        this.name=n;
      }

      public void setOrbitalPeriod(int o)
      {
        this.orbitalPeriod=o;
      }

      public String toString()
      {
        return ("Moon : " +name +" "+"orbit="+orbitalPeriod);
      }

      // This will display the moon when other code is completed.  You don't need to understand this code.
      public void display()
      {
        angle=angle+(0.01*speed);
        pushMatrix();
        rotate(angle);
        translate(distance, 0);
        fill(149, 149, 149);
        ellipse(0, 0, radius*2, radius*2);
        popMatrix();
      }
    }

1 个答案:

答案 0 :(得分:1)

让我们来看看这个函数:

public String getMoonName()
{
   return moons[i].getName();
}

您认为i变量的定义在哪里?你的指示说采取参数,但这个函数不带任何参数。

作为一个小例子,假设我有这个功能:

public void printMessage(){
   println("Hello!");
}

如果我想修改该函数以获取参数,我必须将其添加到这样的方法中:

public void printMessage(String message){
   println(message);
}

您必须使用getMoonName()功能执行类似操作。

如果你仍然被卡住了,please post a small example like mine而不是你的整个草图,我们将从那里开始。