Konvertieren von int zu double Java

Ich bin derzeit mit einem Problem mit meinem code.
Ich habe zu erklären, Stunden und Minuten, wie int, und totalTimeHours als verdoppeln.
totalTimeHours dient zum speichern der Summe der Zeit in Stunden, ab 6.555 Uhr.
Das problem an dem ich arbeite, benötigt die Antwort gefunden werden, in Stunden und Minuten,
zum Beispiel. 6 Stunden und 36 Minuten.
Für mein final run muss ich verwenden 14.7 für Gallonen von gas verwendet werden, und 359.5 für die Ferne.
Ich benutzte Stunden = (int)totalTimeHours zu extrahieren, die ganze Reihe 6
und bin eigentlich zu finden, den Rest der Zeit und multiplizieren Sie diese mit 60 zu finden Minuten,
aber das ist, wo ich mich immer gehalten, bis auf.

Unten ist mein 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);

  }
  • Sie können rufen intValue auf der Doppel-Objekts, um die ganze Zahl.
  • subtrahieren Sie die ganze Zahl ganze Zahl aus dem double zu Holen den Rest.
InformationsquelleAutor Dan S. | 2013-06-06
Schreibe einen Kommentar