2014-02-27 75 views
-1

在這個類中,我有兩個字符串。我想將有序的int與與之相關的星球配對。衛星和每個行星都相互配合。我是否正確配對?如果不是,請告訴我如何做到這一點。在Java中配對兩個獨立字符串中的數據

public class Planet { 

    int moons; 
    String planets; 

    Planet(int moons, String planets) 
    { 
     this.moons = moons; 
     this.planets = planets; 
    } 


    private String planets (String s) {  
     String[] planets = {"Mercury moons = ", "Venus moons = ", "Earth moons = ", "Mars moons = ", "Jupiter moons = ", "Saturn moons = ", "Uranus moons = ", "Neptune moons = ", "Pluto moons = " }; 
     return s;  
    } 


    private int moons (int i) { 
     int[] moons = {0, 0, 1, 2, 67, 62, 27, 14, 5}; 
     return i; 
    } 


} 
+0

您的要求不明確! – shazin

+0

首先刪除'<>'字符中的部分。將這些數組移到「main」方法中 - 它們不屬於您放置它們的位置。並且好好想想你想要這兩種私人方法去做什麼。 –

+0

這裏的OO設計感覺破碎了。爲什麼每個星球都能告訴我「所有」行星的名稱和每個月有多少個衛星? –

回答

2

試試這個,你應該這樣做。

public class Planet 
{ 

static int[] moons; 

static String[] planets; 

Planet(int[] moons, String[] planets) { 
    this.moons = moons; 
    this.planets = planets; 
} 

private static String[] planets() { 
    String[] planets = { "Mercury moons = ", "Venus moons = ", "Earth moons = ", "Mars moons = ", "Jupiter moons = ", "Saturn moons = ", "Uranus moons = ", "Neptune moons = ", "Pluto moons = " }; 
    return planets; 
} 

private static int[] moons() { 
    int[] moons = { 0, 0, 1, 2, 67, 62, 27, 14, 5 }; 
    return moons; 
} 

private void pairPlanet() { 
    if (moons.length == planets.length) { 
     for (int i = 0; i < moons.length; i++) { 
      System.out.println(planets[i] + moons[i]); 
     } 
    } 
    } 
} 

而主要方法去這樣

public static void main(String[] args) { 
    Planet planet = new Planet(Planet.moons(), Planet.planets()); 
    planet.pairPlanet(); 
}