在for循环中更改`double`输出

时间:2014-10-30 07:10:41

标签: java for-loop counter

我想开始说我是新手。我正在尝试制作一个for循环,它会向我展示不同的40码冲刺时间转换为MPH。问题是显示的输出:

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


        }

        }
    }
}

5 个答案:

答案 0 :(得分:3)

问题是您在for循环定义中使用++运算符:

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

将for循环更改为while循环,其中包含每行循环增加FortyTime的行:

while(FortyTime <= 7.99) { 
    FortyTime += 0.01;
    // execute the rest of your code here
}
MarounMaroun正确地指出,使用double作为循环计数器可能会产生令人讨厌的浮点算术错误,因此我已将for循环更改为while循环。

++运算符表示&#34;将x的值重新指定为x + 1。&#34;它只会给你增量(或递减,--)1。

请注意,这将在完成之前打印出数百行。

答案 1 :(得分: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

你不想在你的程序中输出这样的输出。

答案 2 :(得分:1)

在这里,我修改了你打印双打的代码作为循环中产生的两位小数(见格式%.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;
        }
    }
}

答案 3 :(得分:0)

只需更改for循环:

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

并按

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

答案 4 :(得分: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);

以便以正确的精度打印。