我想開始說我是新手。我正在嘗試做一個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.01'添加到'FortyTime'而不是'++'? – Maroun 2014-10-30 07:15:24
另外,請付出很大的關注[Java命名約定](http://www.oracle.com/technetwork/java/codeconventions-135099.html)和更改變量的名稱。 – Maroun 2014-10-30 07:16:10
也許這一個http://stackoverflow.com/questions/3971434/for-loop-increment-by-double可以幫助 – SleepyX667 2014-10-30 07:16:35