I am working on learning Java for a new job and I'm trying to create a simple fuel calculator program. I have to do some calculations and return miles per gallon and liters per 100 KM. The code I have is as follows:
import java.util.Scanner;
class Main {
public static void main (String[] args) {
int fuel;
int distance;
double miles, gallons, litersPerKilo, milesPerGallon;
Scanner inPut = new Scanner(System.in);
System.out.println("Enter the no of liters to fill the tank ");
fuel = inPut.nextInt();
if (fuel<=0){
System.out.println(fuel + " is an Invalid Input");
return;
}
System.out.println("Enter the distance covered ");
distance = inPut.nextInt();
if (distance<=0){
System.out.println(distance + " is an Invalid Input");
return;
} else {
litersPerKilo = (fuel/distance)*100;
miles = (distance*0.6214);
gallons = (fuel*0.2642);
milesPerGallon = miles/gallons;
System.out.println("Liters/100KM");
System.out.println(String.format("%.2f", litersPerKilo));
System.out.println("Miles/gallons");
System.out.println(String.format("%.2f", milesPerGallon));
}
}
}
This mostly works and returns miles per gallon properly. However, it always returns 0.00 for Liters per 100KM. I've tried rearranging things to make sure the calculation is done after the user input, but it is still returning 0. Any suggestions?