我想完成這個程序,它告訴我,在我的主要方法,該fallItem.trackingMethod();
是一個無法言,我想不通爲什麼....不到的語句,和一個永無止境的倒計時
此外,我的倒數計時器應該停止在0,但只是負面的,不會停止,直到我停止了節目。
任何指針表示讚賞,我一直在學習Java中所有的現在5周左右......我必須在這裏失去了一些小細節....
倒計時部分:
System.out.println("Countdown");
for (int i = 5; 1 >= 1; i--) {
System.out.println(i);
}
MyTrajectoryProjector類:
public class MyTrajectoryProjector {
public static double HEIGHT_THRES = 600;
public static double startingPosition() {
Scanner keyboard = new Scanner(System.in);
double aPosition;
do {
System.out.printf("\n\nEnter the initial position (must be over 600.0 feet): ");
aPosition = keyboard.nextDouble();
if ((aPosition <= HEIGHT_THRES)) {
System.out.printf("Error - position too low. Try again.");
}
}
while ((aPosition <= HEIGHT_THRES));
return aPosition;
}
public static double startingVelocity() {
Scanner keyboard = new Scanner(System.in);
double aVelocity; //user entered position
do {
System.out.printf("\n\nEnter the initial velocity (-500.0 ft/sec or more): ");
aVelocity = keyboard.nextDouble();
if ((aVelocity <= MyFallingItem.TERM_VELOC)) {
System.out.printf("Error - velocity too low. Try again.");
}
}
while ((aVelocity <= MyFallingItem.TERM_VELOC));
return aVelocity;
}
public static void main(String[] args) {
System.out.print("This program will calculate the position and velocity "
+ "of a falling object \nuntil it reaches " + HEIGHT_THRES
+ " feet above ground.");
double aPosition = startingPosition();
double aVelocity = startingVelocity();
MyFallingItem fallItem = new MyFallingItem(aPosition, aVelocity);
System.out.printf("\n\n");
System.out.println("Countdown");
for (int i = 5; 1 >= 1; i--) {
System.out.println(i);
}
fallItem.trackingMethod();
System.out.printf("object reached " + HEIGHT_THRES + " feet after "
+ fallItem.getTimeNow() + " seconds. \n The object's final "
+ "position is " + fallItem.getPosNow() + " feet.");
}
}
MyFallingItem類:
public class MyFallingItem {
private final double INI_POS; //needs to be a constant
private final double INI_VEL; //needs to be a constant
private int timeNow; //current time
private double posNow; //current position
private double velNow; //current velocity
public static double TERM_VELOC = -500; //terminal velocity (-500 feet/sec)
//MyFallingItem constructor
public MyFallingItem(double aPosition, double aVelocity) {
this.INI_POS = aPosition;
this.posNow = 0.0;
this.INI_VEL = aVelocity;
this.velNow = 0.0;
this.timeNow = 0;
}
public int getTimeNow() {
return timeNow;
}
public double getPosNow() {
return posNow;
}
public void updateMethod() {
timeNow++;
//V(t) = –32t + V0 (current velocity)
velNow = -32 * timeNow + 0.0;
if (velNow < TERM_VELOC) {
velNow = TERM_VELOC;
}
if (velNow > TERM_VELOC) {
posNow = +500;
} else {
//P(t) = –16t2 + V0t + H0 (current position)
posNow = Math.pow(-16 * timeNow, 2) + INI_VEL * timeNow + INI_POS;
}
}
public void trackingMethod() {
System.out.printf("The initial position is " + INI_POS);
System.out.printf("The initial velocity is " + INI_VEL);
this.updateMethod();
while (this.posNow >= MyTrajectoryProjector.HEIGHT_THRES) {
System.out.printf("Object released from %.1f" + this.INI_POS + " feet at an "
+ "initial velocity of %.1f" + this.INI_VEL + " ft/sec");
this.updateMethod();
System.out.printf(" at " + this.timeNow + " seconds, position is %.1f"
+ posNow + " and velocity is %.1f" + this.velNow + " ft/sec");
}
}
}
花了我一會兒才注意到這個'1> = 1'。我正要發表評論,這兩條線是一樣的:) –
哇,我知道這是簡單的東西...因爲從地獄永無止境的循環,它永遠不會達到'fallItem.trackingMethod();' – Newb2Java