2016-12-02 36 views
-1

我要做的代碼部分有一個對話框,要求用戶輸入一定數量的單位,並要求對象提升輸入數字。
但是,如果單位的數量會導致對象的y位置= 0,那麼會彈出一個警告,並且該對象不會移動。我有一些可行的東西,但只有在對象完成了旅程之後。Java:如何檢查對象在它到達之前的位置

這是我到目前爲止有:

/* 
    * Prompts the user to enter the number of units they want the 
    * rocket to move upwards. 
    * 
    * If the number the units provided would cause the tip of the 
    * capsule to go past the top of the Graphical Display, the user 
    * is informed via a dialogue box that the rocket will not launch. 
    * 
    * Otherwise the rocket launches as required. 
    */ 
    public void launch() 
    { 
     String inputNumString; 
     int inputNum; 
     inputNumString = OUDialog.request("Please Enter Number of Units You Wish the Rocket to Ascend"); 
     inputNum = Integer.parseInt(inputNumString); 
     this.ignition(); 
     this.animateRocket(inputNum); 
     this.getCapsule().getYPos(); 
     if (getCapsule().getYPos() <= 0) 
     { 
     OUDialog.alert("Rocket Will Not Launch"); 



     } 

    } 
+0

提示:不要問/想想非常高的水平的問題,你需要考慮的變量,座標和這些條款。 – zapl

回答

0

if(getCapsule().getYPos() - inputNum <=0)

應該這樣做

+0

對不起,沒有做到這一點。仍然沒有警告 –

0

我不知道有什麼方法做,但你應該你的邏輯改變的東西喜歡這個。

public void launch() 
    { 
     String inputNumString; 
     int inputNum; 
     inputNumString = OUDialog.request("Please Enter Number of Units You Wish the Rocket to Ascend"); 
     inputNum = Integer.parseInt(inputNumString); 
     this.ignition(); 
     // calculate new position here 
     calculateNewPosition(); 
     if (getCapsule().getYPos() <= 0) { 
     OUDialog.alert("Rocket Will Not Launch"); 
     } else { 
     this.animateRocket(inputNum); 
     } 

    } 
+0

這似乎並不奏效:/對不起 –

相關問題