我想开始说我是新手。我正在尝试做一个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