2014-10-30 42 views
1

我想開始說我是新手。我正在嘗試做一個for循環,讓我看到不同的40碼破折號時間轉換爲MPH。問題是,就是BEING顯示輸出:更改循環內的`double`輸出

5.96 40 Time is 13.727882855399633 Miles Per Hour 
6.96 40 Time is 11.755485893416928 Miles Per Hour 
7.96 40 Time is 10.27866605756053 Miles Per Hour 

我想讓它顯示爲5.96,5.97,5.98,等等,而不是5.96和6.96。

有沒有人明白我想要做什麼以及解決我遇到的這個問題?

public class FortyToMPH { 

    public static void main (String args []) { 
     double yards, foot, FeetInMiles, SecondsPerHour,FeetLength; 
     double FortyTime, Minutes, SecondsPerMile, MPH; 
     int counter; 


     counter = 0; 

     for(FortyTime = 4.96; FortyTime <= 7.99; FortyTime++) { 
     yards = 40; // length in yards 
     foot = yards * 3; // convert to feet 
     System.out.println(); 

     FeetInMiles = 5280; // The number of feet in a Mile 
     SecondsPerHour = 3600; 

     FeetLength = FeetInMiles/foot; // You divide the Feet in Miles by the feet conversion of 40 yards 
     System.out.println(); 

     SecondsPerMile = FeetLength * FortyTime; 
     MPH = SecondsPerHour/SecondsPerMile; 
     System.out.println(FortyTime + " 40 Time is " + MPH + " Miles Per Hour "); 

     counter++; 
     // every 10th line, print a blank line 
     if(counter == 10) { 
      System.out.println(); 
      counter = 0; // reset the line counter 


     } 

     } 
    } 
} 
+0

爲什麼不簡單地將'0.01'添加到'FortyTime'而不是'++'? – Maroun 2014-10-30 07:15:24

+1

另外,請付出很大的關注[Java命名約定](http://www.oracle.com/technetwork/java/codeconventions-135099.html)和更改變量的名稱。 – Maroun 2014-10-30 07:16:10

+0

也許這一個http://stackoverflow.com/questions/3971434/for-loop-increment-by-double可以幫助 – SleepyX667 2014-10-30 07:16:35

回答

1

在這裏,我改性你給打印兼作2個小數位數字(見格式%.2f)在迴路中產生的同時(用於循環步驟在DELTA變量被定義)的代碼。

public class FortyToMPH 
{ 
    public static void main (String args []) 
    { 
     double yards, foot, feetLength; 
     double fortyTime = 4.96, minutes, secondsPerMile, mph; 
     int counter = 0; 
     /** 
     * Constants. 
     */  
     final double FEET_IN_MILES = 5280; 
     final double SECONDS_PER_HOUR = 3600; 
     final double DELTA = 0.01; 
     final double END  = 7.99; 

     while (fortyTime <= END) 
     { 
      yards = 40; // length in yards 
      foot = yards * 3; // convert to feet 

      feetLength = FEET_IN_MILES/foot; // You divide the Feet in Miles by the feet conversion of 40 yards 

      secondsPerMile = feetLength * fortyTime; 
      mph = SECONDS_PER_HOUR/secondsPerMile; 
      System.out.format("%.2f 40 Time is %.2f Miles Per Hour%n", fortyTime, mph); 

      counter++; 
      // every 10th line, print a blank line 
      if(counter == 10) { 
       System.out.println(); 
       counter = 0; // reset the line counter 
      } 

      fortyTime += DELTA; 
     } 
    } 
} 
+0

夥計非常感謝。這很棒! – 2014-10-30 07:40:09

+0

我還沒有學過DELTA。雙倍和最終雙倍有什麼區別? – 2014-10-30 07:45:31

+1

這個DELTA是一個常數。這是完全合法的寫入沒有最終。最後的意思是,一旦賦值,變量的值就不能改變。常量應該大寫命名見[Java命名約定(http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-135099.html#367)。 :) – Willmore 2014-10-30 08:16:14

3

的問題是,您使用的++運營商在for循環定義:

for(FortyTime = 4.96; FortyTime <= 7.99; FortyTime++) { 

變化for循環while循環包括線由0.01每次循環遞增FortyTime

while(FortyTime <= 7.99) { 
    FortyTime += 0.01; 
    // execute the rest of your code here 
} 

MarounMaroun正確地指出,使用double作爲循環計數器存在令人討厭的浮點算術錯誤的風險,所以我已經將for循環更改爲while循環。

運算符++表示「重新分配x的值爲x + 1」。它只會給你增量(或遞減,與--)爲1.

請注意,這將打印出數百行之前完成。

+1

注意浮點運算的! – Maroun 2014-10-30 07:20:22

+0

@MarounMaroun嗯,我在這裏撓我的頭 - 給我開光?是否有時候只有兩位數的精確度將百分數加到任何數字都會導致舍入誤差? – furkle 2014-10-30 07:21:36

+0

是的,試着打印這個循環的值:'for(double i = 0; i <1.0; i + = 0.1)'。 – Maroun 2014-10-30 07:25:29

0

只是改變for循環是這樣的:

for(FortyTime = 4.96; FortyTime <= 7.99; FortyTime=FortyTime+.01) 

,並打印它像

System.out.println((float)FortyTime + " 40 Time is " + MPH + " Miles Per Hour "); 
0

你需要做出兩個改變:

for(FortyTime = 4.96; FortyTime <= 7.99; FortyTime=FortyTime+0.01) 

從而使FortyTime遞增0.01而不是1和

System.out.printf("%.2f 40 Time is %f Miles Per Hour ", FortyTime, MPH); 

因此它以正確的精度打印。

2

我會建議你使用你的循環int並執行循環內的double一切都計算,以防止問題與浮點運算:

for(int i = 0; i < something; i++) { 
    double fortyTime = 4.96 + i; 
    //... 
} 

也請注意Java Naming Conventions和重命名你的變量。

爲了證明浮點運算的問題,試試這個循環:

for(double i = 0; i < 1.0; i += 0.1) 
    System.out.println(i); 

這將打印

0.0 
0.1 
0.2 
0.30000000000000004 
0.4 
0.5 
0.6 
0.7 
0.7999999999999999 
0.8999999999999999 
0.9999999999999999 

而且你不想在你的程序是這樣的輸出。