배열은 String
입니다. 모든 객체를 인쇄하려면 for
루프를 사용하고 싶습니다. 나는 루프를 만들고 나서 String
을 반환함으로써 이것이 이루어질 것이라는 인상을 받고 있었다. 나는 이것을 성취하기 위해 내가 무엇을 바꿔야하는지 확신하지 못한다. 다음 내 노력 :for 루프를 사용하여 모든 배열을 Java로 인쇄하는 방법
public class SolarSystem {
private Planet[] planets;
private int position = 0;
public SolarSystem(int size) {
planets = new Planet[size];
}
public void add(Planet planet) {
planets[position] = planet;
position++;
}
public String toString(){
for(int i = 0; i < planets.length; i++){
}
return toString();
}
}
ADDED PLANET CLASS
public class Planet {
String name;
int moons;
public Planet(String name, int moons)
{
this.moons = moons;
this.name = name;
}
public String toString() {
return "The Planet " + name + " Has " + moons + " Moon(s) \r\n ";
}
}
'System.out.println (planets [i])'doesn 격차를 메우지 않습니까? – ItachiUchiha