2015-11-03 46 views
-1

嗨,大家好,抱歉,如果這是一個常見的和令人討厭的問題,我試圖做一個父類(3個單獨的類),但我真的不知道該從哪裏去。創建和比較兒童類

我需要它們在0 < = x < = 100和0 < = y < = 100的限制範圍內隨機產卵,並且不能超過1個小數點。 (我將需要能夠比較'兒童'班級彼此之間的距離)。

或者我不需要3個新班級,但是我可以在同一班級完成所有任務嗎?

如果您認爲我需要,也請隨時添加建議。

TIA

public class Soldier { 
    public static void main(String args[]) { 

      Random random = new Random(); 
      double max = 100; //sets the limit to 100 for the soldiers spawn 
      double min = 0; //sets the minimum to 0 for the soldiers spawn 
      double xPos = Math.random() * (max - min) - min;//for the x coordinate of the soldiers position 
      double yPos = Math.random() * (max - min) - min; //for the y coordinate of the soldiers position 
      xPos = Math.round(xPos * 10)/10.0d; //making sure the x value is to 1dp 
      yPos = Math.round(yPos * 10)/10.0d; //making sure the y value is to 1dp 
      System.out.println("(" + xPos + " ," + yPos + ")"); //printing out the position of the soldier (x, y) 

     } 

    } 
+1

你的問題似乎至少包括3個問題,其中0個問題表達得足夠清楚,足以給你一個正確的答案。你試圖解決的實際問題是什麼?是什麼讓你思考孩子班? – zapl

回答

0

我不知道,如果父母與孩子有什麼關係,但這裏是我想你想:

public static void main(String ... args){ 
    double min = 0; 
    double max = 100; 

    Point p1 = generatePoint(min, max); 
    Point p2 = generatePoint(min, max); 
    Point p3 = generatePoint(min, max); 
} 

public static Point generatePoint(double min, double max){ 
    double xPos = Math.random() * (max - min) - min; 
    double yPos = Math.random() * (max - min) - min; 

    return new Point((int) xPos, (int) yPos); 
} 
+0

感謝您的幫助,最後一件事,我如何使這個雙而不是int? return new Point((int)xPos,(int)yPos); – Manderson

0

我不認爲你需要3個類,只有3個這個類的對象。我會爲該類添加一個「id」成員,以便您可以區分它們。我還會添加幾個全班級的靜態成員,其中一個用於跟蹤我們所處的「身份證號」,因爲我們會向更多的士兵實例,並且可能會有一些指向這些實例的指針,以便於查找和操作實例。