nmanpure
June 25th, 2010, 20:09
This will simply figure the price to fill your tank (from empty) or how much it will cost to travels a certain distance in miles.
Code:
Main.Java
package gascost;
import java.util.Scanner;
/**
*
* @author Noah Schweibinz
*/
public class Main
{
public static void main(String[] args)
{
Scanner keybd = new Scanner(System.in);
double cost=0.0;
double gallons=0, master, average, miles;
int task=0;
System.out.println("Enter price of gas per Gallon: ");
cost = keybd.nextDouble();
System.out.println("Would you like to find cost per tank fill or trip cost?\nsay 1 for tank or 2 for trip: ");
task = keybd.nextInt();
if(task == 1)
{
System.out.println("Enter how many Gallons your tank can hold: ");
gallons = keybd.nextDouble();
master = cost * gallons;
System.out.println("It will cost you "+ master * 1.0 + " To fill your Tank");
}else if(task == 2){
System.out.println("How many miles per gallon do you average: ");
average = keybd.nextDouble();
System.out.println("How many miles are you going to travel: ");
miles = keybd.nextDouble();
master = miles * cost / average;
System.out.println("Your trip will cost: "+master);
}
}
}
Code:
Main.Java
package gascost;
import java.util.Scanner;
/**
*
* @author Noah Schweibinz
*/
public class Main
{
public static void main(String[] args)
{
Scanner keybd = new Scanner(System.in);
double cost=0.0;
double gallons=0, master, average, miles;
int task=0;
System.out.println("Enter price of gas per Gallon: ");
cost = keybd.nextDouble();
System.out.println("Would you like to find cost per tank fill or trip cost?\nsay 1 for tank or 2 for trip: ");
task = keybd.nextInt();
if(task == 1)
{
System.out.println("Enter how many Gallons your tank can hold: ");
gallons = keybd.nextDouble();
master = cost * gallons;
System.out.println("It will cost you "+ master * 1.0 + " To fill your Tank");
}else if(task == 2){
System.out.println("How many miles per gallon do you average: ");
average = keybd.nextDouble();
System.out.println("How many miles are you going to travel: ");
miles = keybd.nextDouble();
master = miles * cost / average;
System.out.println("Your trip will cost: "+master);
}
}
}