使用十进制格式的'\ n'和'\ t'转义序列?

时间:2017-03-12 18:41:13

标签: java

我的在线编程课程的任务是编写一个简短的程序来计算您在餐厅的账单。这张餐馆账单应该显示税前的餐费(自己补足),协调销售税(13%)以及含税的餐费。我决定添加提示以添加更多细节。

程序的输出应格式化为2位小数。我应该使用/ n和/ t转义序列?我将使用哪些代码以及我将它放在哪里?

这是我的代码:

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 个答案:

答案 0 :(得分:1)

尝试

System.out.println(String.format("Meal Charge:................... $%.2f" , mealCharge));