Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将int转换为双Java_Java_Casting_Int_Double - Fatal编程技术网

将int转换为双Java

将int转换为双Java,java,casting,int,double,Java,Casting,Int,Double,我目前的代码有问题。 我必须将小时和分钟声明为int,将totalTimeHours声明为double。 totalTimeHours用于以小时为单位存储总时间,例如6.555小时。 我正在研究的问题需要在数小时和数分钟内找到答案, 例如6小时36分钟。 在我的最后一次跑步中,我需要使用14.7加仑的汽油,359.5加仑的距离。 我使用hours=(int)totalTimeHours来提取整数6 我应该找到剩余的时间,然后乘以60,得到分钟数, 但这就是我被耽搁的地方 下面是我的代码: p

我目前的代码有问题。 我必须将小时和分钟声明为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 dTime = 6.555;
    int hours = (int) dTime;
    int minutes = (int) ((dTime - hours) * 60);
    System.out.println(hours + ":" + minutes); // prints 6:33

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

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

更换

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

您正在传入一个双参数,因此您调用返回long的参数,并将其指定给int,这是错误的。

替换

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

你在传递一个双参数,所以你调用一个返回一个long的参数,然后把它赋给一个int,这是错误的。

您可以对Double对象调用intValue来获得整数。从Double对象中减去整数可以得到余数。您可以对Double对象调用intValue来获得整数。从Double对象中减去整数可以得到余数。这是可以编译的,但几分钟后的答案是35。所以我尝试使用:System.out.println(“小时和分钟驱动:+hours+Math.round(minutes))//但我还有35分钟。我需要6小时36分钟。我想知道为什么数学。四舍五入(分钟)没有四舍五入到36。@user2460398哦,我明白了。所以如果你有35.6,你想得到36,如果你有35.4,你想得到35?数学四舍五入((浮动)((totalTimeHours-hours)*60))。数学。舍入(分钟)不是舍入,因为分钟是整数,你不能舍入整数。非常感谢。我使用了:分钟=(int)数学轮((totalTimeHours-hours)*60)//并且能够让它以期望的结果运行。我试着在int和Math的不同组合中使用试错法。再次感谢!!这是可以编译的,但几分钟后的答案是35。所以我尝试使用:System.out.println(“小时和分钟驱动:+hours+Math.round(minutes))//但我还有35分钟。我需要6小时36分钟。我想知道为什么数学。四舍五入(分钟)没有四舍五入到36。@user2460398哦,我明白了。所以如果你有35.6,你想得到36,如果你有35.4,你想得到35?数学四舍五入((浮动)((totalTimeHours-hours)*60))。数学。舍入(分钟)不是舍入,因为分钟是整数,你不能舍入整数。非常感谢。我使用了:分钟=(int)数学轮((totalTimeHours-hours)*60)//并且能够让它以期望的结果运行。我试着在int和Math的不同组合中使用试错法。再次感谢!!