Wie man die division von 2 Ganzzahlen erzeugen ein schweben anstatt eines anderen int?

In einem anderen Bruce Eckels übung in der Berechnung von Geschwindigkeit, v = s /t wo s und t ganze zahlen sind. Wie mache ich es, sodass die division Kurbeln aus einem Schwimmer?

class CalcV {
  float v;
  float calcV(int s, int t) {
    v = s / t;
    return v;
  } //end calcV
}

public class PassObject {

  public static void main (String[] args ) {
    int distance;
    distance = 4;

    int t;
    t = 3;

    float outV;

    CalcV v = new CalcV();
    outV = v.calcV(distance, t);

    System.out.println("velocity : " + outV);
  } //end main
}//end class
float v=s/t führt die division dann wandelt das Ergebnis in einen float. float v=(float)N/t-casts auf float dann führt die division.

InformationsquelleAutor phill | 2009-04-24

Schreibe einen Kommentar