java - 调试 Java 餐价计算器
问题描述
我在调试此代码时遇到了一些问题,希望大家能够提供帮助。
如果你看一下main
方法接近尾声,你会发现int partySplit.split.nextInt()
从Scanner split = new Scanner(System.in);
. 第一个问题是程序似乎跳过了这一点。为什么要跳过这组代码?
第二个问题是它会打印出“总餐价为 128 美元”,但我已经删除了System.out.println("Total meal price is $" + groupTotalMealPrice);
我还重建了程序(使用 IntelliJ),看看它是否只是一个编译问题。我也复制并粘贴到一个新的类文件中,我得到了相同的结果。
我想看到的是它询问聚会的人数,然后不仅打印出总数,还打印出每个人的总数,具体取决于拆分。我也希望看到我删除的文本实际上消失了。
对此的任何帮助都会很棒。
代码:
import java.text.DecimalFormat;
import java.util.Scanner;
public class MealPriceCalculator {
//note 'double' in 'public static double' is the return type.
//notice that is has been changed from 'void' which means return nothing.
//when this is changed, we will need to denote a return value.
//in this case, we are returning the result to the main method.
//when returning a variable, you are making it accessible in another method.
public static double calculateTotalMealPrice(double listedMealPrice,
double tipRate,
double taxRate) {
double tip = (tipRate * listedMealPrice) / 100;
double tax = (taxRate * listedMealPrice) / 100;
double result = listedMealPrice + tip + tax;
return result;
}
//note 'void' in 'public static void' is the return type.
//this means that nothing is returned.
public static void main(String[] args) {
Scanner mealPrice = new Scanner(System.in);
Scanner tip = new Scanner(System.in);
Scanner tax = new Scanner(System.in);
Scanner split = new Scanner(System.in);
System.out.println("Please enter the meal price (excluding $):");
double listedMealPrice = mealPrice.nextDouble();
System.out.println("You entered $" + listedMealPrice + " as the cost of your meal.");
System.out.println("Please enter the tip percentage (excluding %)");
double tipRate = tip.nextDouble();
System.out.println("You entered " + tipRate + "% as the percentage you wish to tip.");
System.out.println("Please enter the tax rate (excluding the %):");
double taxRate = tax.nextDouble();
System.out.println("You entered " + taxRate + "% as the tax rate.");
System.out.println("Please enter the number of people that will be splitting the bill.");
int partySplit = split.nextInt();
System.out.println("You entered " + partySplit + " as the number of people you will be" +
"splitting the bill with.");
double groupTotalMealPrice = calculateTotalMealPrice(listedMealPrice, tipRate, taxRate);
System.out.println(groupTotalMealPrice);
double individualMealPrice = groupTotalMealPrice / partySplit;
System.out.println(individualMealPrice);
}
}
结果:
Please enter the meal price (exluding $):
100
You entered $100.0 as the cost of your meal.
Please enter the tip percentage (excluding %)
20
You entered 20.0% as the percentage you wish to tip.
Please enter the tax rate (excluding the %):
8
You entered 8.0% as the tax rate.
Your total meal price is $128.0
Process finished with exit code 0
解决方案
您可以使用一个对象来获取所有输入,而不是为每个输入使用每个 Scanner 对象
Scanner sc= new Scanner(System.in);
double listedMealPrice = sc.nextDouble();
double tipRate = sc.nextDouble();
double taxRate = sc.nextDouble();
推荐阅读
- django - 模型上的日期验证 - 时间/日期冲突
- azure - azure logic app 如何检查 sftp 中的特定文件已更改
- c# - Resharper - 更喜欢带有支持字段的代码样式
- javascript - 如何从多行和多列的表格中获取选定的复选框并作为回复发送
- android - 如何测试 androidx.fragment.app.Fragment.onCreateOptionsMenu()?
- sql - 在 mongodb 上进行 sql 查询以根据单列的 id 值获取多列的计数
- tensorflow - CNN - 在 1 张图片中显示的两个类别之间进行选择
- excel - 在代码问题中使用 Union 将工作表与源工作表分开
- python - 如何将元组值添加到值也是 nd.array 的字典的每个键中?
- java - 当它从java中的内部“for”循环“中断”时它会“更新”吗?