我正在尝试制作一个程序来计算公路旅行的mpg。我已经把它全部布置好了,但每次运行我的 mpg 都会得到 0.0。我可以使用一些帮助。我必须制作 mpg 和 totalMpg 浮点数,这样我才能进行浮点除法,如果这没有意义就这么说吧。
package Gas;
import java.util.Scanner;
import static java.lang.System.out;
public class GasTest {
public static void main(String[] args) {
int gas;
int miles;
int trips;
float mpg;
float totalMpg = 0;
int tripCounter = 1;
Scanner input = new Scanner (System.in);
out.println("Please input number of trips");
trips = input.nextInt();
while (tripCounter <= trips){
out.println ("Input gallons of gas used");
gas = input.nextInt();
out.println("Input miles traveled on trip");
miles = input.nextInt();
mpg = (gas / miles) ;
out.println("Your MPG for this trip was " + mpg);
totalMpg = (mpg + totalMpg) / 2;
out.println("Your overall MPG for all trips is " + totalMpg);
tripCounter++;
}
}
}