-1

When running my program, the last line displaying yearly gas expense is not showing... what have I done wrong? Also would like some help rounding figures to 2 decimal points... I tried implementing a few strategies but none of them worked. Any help is appreciated!

public class GasExpenses {
public static void main(String[] args) {

//Use scanner in order to retrieve user input.
Scanner keyboard= new Scanner(System.in);

//Declare variables.
int milesPerWeek;
double milesPerGallon,costOfGas;


System.out.println("GAS EXPENSES"); 
//Get user input.
System.out.println("How many miles do you drive a week?");
milesPerWeek = keyboard.nextInt();

//Get user input.
System.out.println("How many miles per gallon does your auto get?");
milesPerGallon=keyboard.nextDouble();

//Get user input.
System.out.println("What is the current cost of gas?");
costOfGas=keyboard.nextDouble();

//Calculate miles per year.
int milesPerYear=(milesPerWeek*52);
//Display calculated yearly mileage.
System.out.println("At " + milesPerWeek + "miles per week, you travel "+
milesPerYear + " miles per year.");

//Calculate and display calculated weekly & yearly gallons of gas used.
double gallonsPerWeek=(milesPerWeek/milesPerGallon); 
System.out.println("Gallons per week:" + gallonsPerWeek);
double gallonsPerYear=(gallonsPerWeek*52);
System.out.println("Gallons per year:" + gallonsPerYear);

//Calculate and display calculated weekly & yearly gas expense.
System.out.println("With gas at $" +costOfGas + " per gallon, you will
spend: ");
double weeklyGasExpense=(costOfGas*milesPerWeek);
System.out.println("Gas expense per week: $" +weeklyGasExpense);
double yearlyGasExpense=(costOfGas*milesPerYear);
System.out.println("Gas expense per year: $" +yearlyGasExpense);

//Calculate and display calculated weekly & yearly gas expense based on
increased gas price.
double costIncrease= (costOfGas+1);
System.out.println("If gas goes up by one dollar per gallon to $" +
costIncrease +(" per gallon, you will spend:"));
double weeklyIncreasedGas=(costIncrease*milesPerWeek);
double yearlyIncreasedGas=(costIncrease*milesPerYear);
System.out.println("Gas expense per week : $" +weeklyIncreasedGas);
System.out.print("Gas expense per year : $" +yearlyIncreasedGas);
}}
Jay Cee
  • 1
  • 1
  • are you missing the end of your code? Can't see no termination `}`s – Scary Wombat Oct 14 '16 at 01:04
  • `double round = Math.round(a * 100.0) / 100.0;` for your second question – Athamas Oct 14 '16 at 01:07
  • Please carefully read [MCVE] guidance on what should be posted as sample code and ask *one* question per post. (Note that there is some chance that other people already tried to format numbers that way before - make sure to search first). – Alexei Levenkov Oct 14 '16 at 01:07
  • i didn't paste them ... let me edit. they were in the original code, though. – Jay Cee Oct 14 '16 at 01:10

2 Answers2

0

Run system.out.flush() at the end of your code to make sure everything is printed.

For the rounding you could try this answer round up to 2 decimal places in java?

Community
  • 1
  • 1
diegowc
  • 455
  • 2
  • 14
0

For first question, this line works for me given your code:

System.out.print("Gas expense per year : $" +yearlyIncreasedGas);

For second do this:

double number = 111.12345;
double result = Math.round(number * 100.0) / 100.0;
Anton Kim
  • 879
  • 13
  • 36