为啥我的代码的最后一行没有打印?另外,我将数字四舍五入到小数点后两位的最简单方法是啥? [复制]
Posted
技术标签:
【中文标题】为啥我的代码的最后一行没有打印?另外,我将数字四舍五入到小数点后两位的最简单方法是啥? [复制]【英文标题】:Why is the last line of my code not printing? Also, what's the simplest way for me to round numbers to 2 decimals? [duplicate]为什么我的代码的最后一行没有打印?另外,我将数字四舍五入到小数点后两位的最简单方法是什么? [复制] 【发布时间】:2016-10-14 01:02:24 【问题描述】:在运行我的程序时,显示每年 gas 费用的最后一行没有显示...我做错了什么?还需要一些帮助将数字四舍五入到小数点后 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);
【问题讨论】:
您是否遗漏了代码的结尾?看不到没有终止
s
double round = Math.round(a * 100.0) / 100.0;
你的第二个问题
请仔细阅读minimal reproducible example 指南,了解应作为示例代码发布的内容,并针对每个帖子提出一个 问题。 (请注意,其他人之前可能已经尝试过以这种方式格式化数字 - 请务必先搜索)。
我没有粘贴它们...让我编辑。不过,它们在原始代码中。
【参考方案1】:
在代码末尾运行 system.out.flush() 以确保打印所有内容。
对于四舍五入,你可以试试这个答案round up to 2 decimal places in java?
【讨论】:
【参考方案2】:对于第一个问题,鉴于您的代码,这一行对我有用:
System.out.print("Gas expense per year : $" +yearlyIncreasedGas);
第二次这样做:
double number = 111.12345;
double result = Math.round(number * 100.0) / 100.0;
【讨论】:
以上是关于为啥我的代码的最后一行没有打印?另外,我将数字四舍五入到小数点后两位的最简单方法是啥? [复制]的主要内容,如果未能解决你的问题,请参考以下文章
所以我做了一个线性搜索代码,但是当我在数组中输入一个数字时,它仍然会打印(数字没有)为啥会这样?
为啥我不能在 python 诅咒窗口中将str()添加到最后一行/列?