Where did I messed up? Why is my method returning Infinity?

Trần Lam :
public class Vehicle {
    int passengers;
    int fuelcap;
    int mph;
    Vehicle(int p, int f, int m) {
         p = passengers;
         f = fuelcap;
         m = mph;
    }
    double fuelNeeded (int miles) {
        return (double) miles/mph;
    }
}
public class STUFF {
    public static void main(String[] args) {
        Vehicle minivan = new Vehicle(7, 16, 21);
        Vehicle sportscar = new Vehicle(2, 14, 12);
        double gallons;
        int dist = 252;
        gallons = minivan.fuelNeeded(dist);
        System.out.println(gallons);
        gallons = sportscar.fuelNeeded(dist);
        System.out.println(gallons);

    }
}
Output:
Infinity
Infinity

I have been stuck on this problem for quite sometimes now, I'm not sure where I messed up but the method keeps outputing the result as Infinity, it would be much helpful if you guys can give me some insights to where and how the code was wrong. Thank you so much!!

Schred :

You are assigning the local variables (p, f, m) the values of your global variables, but you should be doing it the other way around:

Vehicle(int p, int f, int m) {
     passengers = p;
     fuelcap = f;
     mph = m;
}

Because of this, you are dividing by 0 in fuelNeeded, which results in Infinity.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=409744&siteId=1