【问题标题】:Convert int to double Java将 int 转换为 double Java
【发布时间】:2013-06-02 18:40:49
【问题描述】:

我的代码目前有问题。 我必须将小时和分钟声明为 int,将 totalTimeHours 声明为 double。 totalTimeHours 用于以小时为单位存储总时间,例如 6.555 小时。 我正在处理的问题需要在几小时和几分钟内找到答案, 例如。 6小时36分钟。 对于我的最后一次跑步,我需要使用 14.7 加仑的汽油和 359.5 的距离。 我用 hours = (int)totalTimeHours 来提取整数 6 我应该找到剩余的时间并将其乘以 60 以找到分钟, 但这就是我被耽搁的地方。

下面是我的代码:

  public static void computeMilesPerGallon()
  {   //start brackett for computeMilesPerGallon

     double gallons, distance, mpg, totalTimeHours;   //totalTimeHours is the total  time in just hours
                                                                         //for example totalTimeHours = 6.5555 hrs
     int minutes, hours;
     final String DASHES = "-----------------------------------------------";
     final double AVERAGE_SPEED = 54.5;     

     DecimalFormat oneDecimalDigits = new DecimalFormat ("0.0");   //prints decimal to 1 places
     DecimalFormat threeDecimalDigits = new DecimalFormat ("0.000");   //prints decimal to 3 places  

     System.out.println ("\n\t\t\tSpeed Problem");
     System.out.println ("\t\t\t-------------");
     System.out.print ("\n\t\tEnter in the gallons of gas used: ");   //gets gallons of gas used
     gallons = scan.nextDouble();

     System.out.print ("\t\tEnter in the total distance driven: ");   //gets total distance driven
     distance = scan.nextDouble();

     mpg = distance / gallons;   //calculates mpg
     System.out.println ("\t\tThis is your miles per gallon (mpg): " + oneDecimalDigits.format(mpg));  
                                                                         //displays mpg
    //calculates time// 
     totalTimeHours = distance / AVERAGE_SPEED;

    //below line displays total time in hours to 3 decimal places
     System.out.println ("\n\t\tThis is was your time in hours: " 
                                        + threeDecimalDigits.format(totalTimeHours));

    //extracts hours from time in hours
     hours = (int)totalTimeHours;
        minutes = Math.round((totalTimeHours - hours) * 60);

    //prints total time in hrs and minutes
     System.out.println ("\t\tThis is the total time in hours and minutes: " + hours 
                                            + " hours and " + minutes + " minutes");  

     System.out.println ("\t" + DASHES);

  }

【问题讨论】:

  • 您可以在 Double 对象上调用 intValue 来获取整数。
  • 从双精度数中减去整数得到余数。

标签: java casting int double


【解决方案1】:

替换

minutes = Math.round((totalTimeHours - hours) * 60);

minutes = Math.round((float)((totalTimeHours - hours) * 60));

minutes = (int)Math.round((totalTimeHours - hours) * 60);

数学中有两种方法

long Math.round(double);
int Math.round(float);

你传入了一个 double 参数,所以你调用返回 long 的那个,然后将它分配给一个 int ,这是错误的。

【讨论】:

  • 这可以编译,但以分钟为单位的答案是 35。所以我尝试使用: System.out.println ("hours and minutes drive:" + hours + Math.round(minutes)) ; //但我还有 35 分钟。我需要有 6 小时 36 分钟。我想知道为什么 Math.round(minutes) 没有四舍五入到 36。
  • @user2460398 哦,我明白了。所以如果你有 35.6 你想得到 36 ,如果你有 35.4 你想得到 35 ? Math.round((float)((totalTimeHours - hours) * 60)); . Math.round(minutes) 不四舍五入,因为分钟是整数,不能四舍五入。
  • 非常感谢。我用过:分钟 = (int)Math.round((totalTimeHours - hours) * 60); //并且能够让它以预期的结果运行。我试图在 int 和 Math.round 的不同组合中使用试错法,我没有考虑上面的语法。再次感谢!!
【解决方案2】:

我没有阅读您的所有代码,但这应该有助于解决您在文本中描述的问题:

    double dTime = 6.555;
    int hours = (int) dTime;
    int minutes = (int) ((dTime - hours) * 60);
    System.out.println(hours + ":" + minutes); // prints 6:33

【讨论】:

    猜你喜欢
    • 2011-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-14
    • 2013-11-23
    • 1970-01-01
    相关资源
    最近更新 更多