My task for my online programming class is to write a short program that calculates your bill at a restaurant. This restaurant bill is supposed to display the cost of the meal before taxes (make up your own amount), Harmonized Sales Tax (13%) and the cost of the meal including taxes. I decided to add the tip in just to add more detail.
我的在线编程课程的任务是编写一个简短的程序来计算您在餐厅的账单。这个餐馆账单应该显示税前的餐费(自己补足),协调销售税(13%)以及含税的餐费。我决定添加提示只是为了添加更多细节。
The output of the program should be formatted to 2 decimal places. I am supposed to use the /n and /t escape sequences? What code am I to use for this and where would I put it?
程序的输出应格式化为2位小数。我应该使用/ n和/ t转义序列?我将使用什么代码用于此以及我将它放在哪里?
Here is my code:
这是我的代码:
public static void main(String[] args) {
double mealCharge;
double tax;
double tip;
double total;
try (
Scanner keyboard = new Scanner(System.in)) {
System.out.print("Enter the charge for the meal: ");
mealCharge = keyboard.nextDouble();
DecimalFormat df = new DecimalFormat("\"%.2f");
// Calculate the tax.
tax = calculateTax (mealCharge);
// Calculate the tip.
tip = calculateTip (mealCharge);
// Calculate the total.
total = calculateTotal (mealCharge, tax, tip);
// Display the results.
System.out.println("Meal Charge:................... $" + mealCharge);
System.out.println("Tax:............................$" + tax);
System.out.println("Tip:............................$" + tip);
System.out.println("Total:..........................$" + total);
}
}
static double calculateTax (double mealCharge) {
return mealCharge * TAX_RATE;
}
static double calculateTip (double mealCharge) {
return mealCharge * TIP_PERCENT;
}
static double calculateTotal (double mealCharge, double tax, double tip) {
return mealCharge + tax + tip;
}
}
1 个解决方案
#1
1
Try
System.out.println(String.format("Meal Charge:................... $%.2f" , mealCharge));
#1
1
Try
System.out.println(String.format("Meal Charge:................... $%.2f" , mealCharge));