我要做的代码部分有一个对话框,要求用户输入多个单位,并输出一个对象来提升输入数字。
但是,如果单位数量会导致物体的y位置= 0,则会弹出警报并且物体不会移动。我有一些有用的东西,但只有在物体完成它之后才会有效。
这是我到目前为止所做的:
/*
* 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 :(得分:0)
if(getCapsule().getYPos() - inputNum <=0)
应该这样做
答案 1 :(得分: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);
}
}