简体   繁体   English

将int转换为double Java

[英]Convert int to double Java

I'm currently having an issue with my code. 我的代码目前有问题。 I have to declare hours and minutes as int, and totalTimeHours as double. 我必须将小时和分钟声明为int,将totalTimeHours声明为double。 totalTimeHours is used to store the total time in hours, ex 6.555 hrs. totalTimeHours用于存储以小时为单位的总时间,例如6.555小时。 The problem I'm working on requires the answer to be found in hours and minutes, for example. 例如,我正在处理的问题需要在几小时和几分钟内找到答案。 6 hours and 36 minutes. 6小时36分钟。 For my final run, I need to use 14.7 for gallons of gas used and 359.5 for distance. 对于我的最后一次运行,我需要使用14.7加仑汽油和359.5加仑汽油。 I used hours = (int)totalTimeHours to extract the whole number 6 and am supposed to find the remainder of time left over and multiply that by 60 to find minutes, but that's where I'm getting held up on. 我使用小时=(int)totalTimeHours提取整数6,并应该找到剩余的剩余时间,然后将其乘以60以找到分钟,但这就是我要坚持的时间。

Below is my code: 下面是我的代码:

  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);

  }

Replace : 更换

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

With :

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

or 要么

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

There are two methods round in Math 数学中有两种方法

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

You are passing in a double argument so you call the one that returnes a long and you assign it to an int ,thats wrong. 您传入了double参数,因此您调用了返回long的参数,并将其分配给int,那是错误的。

I didn't read all of your code, but that should help solve the problem you described in your text: 我没有阅读所有代码,但这应该可以解决您在文字中描述的问题:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM