2016-02-14 63 views
1

我想完成這個程序,它告訴我,在我的主要方法,該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"); 

     } 
    } 

} 

回答

4

替換

for (int i = 5; 1 >= 1; i--) 

與:

for (int i = 5; i >= 1; i--) 

在for循環中的第二個參數是所需條件。您定義的條件是:1 >= 1總是爲true。

您忘了更改條件,因此它會檢查變量i的值,因此,您正在輸入一個無限循環,這並不好!


Quick reference about for loops in java.

+0

花了我一會兒才注意到這個'1> = 1'。我正要發表評論,這兩條線是一樣的:) –

+0

哇,我知道這是簡單的東西...因爲從地獄永無止境的循環,它永遠不會達到'fallItem.trackingMethod();' – Newb2Java

1

@ Evin1_給你正確的代碼(編輯:現在增加了一個解釋),但我想提供一個解釋。

他是對的:for (int i = 5; 1 >= 1; i--)是破碎的代碼,但這是爲什麼。控制循環運行多長時間的表達式,1>=1,是總是爲真。因爲你有這個,你基本上是寫一個while(true)循環,在年底遞減i,這就是爲什麼

[它]只是爲負,也不會停止,直到我打停在程序

+0

我看到,感謝你們提供的信息,它仍然令人驚訝,你必須非常詳細。我正盯着這看起來像是永遠的東西,沒有看到它 – Newb2Java

+2

@ Newb2Java我,j,l,1是麻煩的來源 – emory