2014-10-30 118 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 

而且你不想在你的程序是这样的输出。