java - Java 将 int 转换为 double

标签 java casting int double

我的代码当前遇到问题。 我必须将小时和分钟声明为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);

  }

最佳答案

替换:

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

:

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

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

Math中有两种方法

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

您传递了一个双参数,因此您调用返回 long 的参数并将其分配给 int ,这是错误的。

关于java - Java 将 int 转换为 double,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16967459/

相关文章:

java - 我不明白如何让我的程序从整数返回 int 值

java - 为什么我不能一次初始化多个数组?

java - 通过在 Java 项目中删除 XML,您的效率提高了多少?

java - Kraken 交换私有(private)请求

c - 将字符串解析为 int 数组 - 收到警告 "makes pointer from integer without a cast"

c - 为 char * 和 int 的 sprintf 分配内存

java - 数组改组不起作用

input - 如何在 Go 中使用 strconv.Atoi() 方法?

java - 将字符串转换为 double 然后再转换回来

Python,如何将现有的父类对象转换为子类对象