2016-10-14 9 views
-1

运行我的程序时,显示年度天然气费用的最后一行没有显示......我做错了什么?也想一些帮助四舍五入到小数点后两位... 我试图实施一些策略,但没有一个工作。 任何帮助表示赞赏!为什么我的代码的最后一行不打印?另外,对于我来说,将数字舍入到2位小数最简单的方法是什么?

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); 
}} 
+0

你错过了代码的结尾?无法看到终止'} –

+0

'double round = Math.round(a * 100.0)/ 100.0;'您的第二个问题 – Athamas

+0

请仔细阅读[MCVE]关于应该作为示例代码发布的指导,并询问*每个帖子有一个*问题。 (请注意,其他人已经尝试过以前的方式对数字进行格式化 - 请确保先搜索)。 –

回答

0

对于第一个问题,此行对我的作品给你的代码:

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

对于第二个做到这一点:

double number = 111.12345; 
double result = Math.round(number * 100.0)/100.0; 
相关问题