У меня в настоящее время проблема с моим кодом. Я должен объявить часы и минуты, как int, и totalTimeHours как double. totalTimeHours используется для хранения общего времени в часах, ex 6.555 hrs. Проблема, над которой я работаю, требует ответа, который можно найти в часах и минутах, например, . 6 часов и 36 минут. Для моего финального пробега мне нужно использовать 14,7 для галлонов используемого газа и 359,5 для расстояния. Я использовал часы = (int) totalTimeHours для извлечения всего числа 6 , и я должен найти оставшееся время, оставшееся и умножить на 60, чтобы найти минуты, , но это то место, где меня задерживают.Преобразование int, чтобы удвоить Java
Ниже мой код:
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);
}
вы можете вызвать intValue на объекте Double, чтобы получить целое число. – NeplatnyUdaj
вычесть целое число из числа double для получения остатка. – jtahlborn