Java是一種很強大的編程語言,它可以被用來實現各種各樣的應用程序,例如計算小費和消費率等。今天我們來看一看如何使用Java來計算小費和消費率。
public class TipAndTaxCalculator{ public static void main(String[] args){ double mealCost = 35.50; double tipPercent = 0.15; double taxPercent = 0.0675; double tipAmount = mealCost * tipPercent; double taxAmount = mealCost * taxPercent; double totalCost = mealCost + tipAmount + taxAmount; System.out.println("The cost of the meal is " + mealCost); System.out.println("The tip is " + tipAmount); System.out.println("The tax is " + taxAmount); System.out.println("The total cost is " + totalCost); } }
代碼中聲明了三個double類型的變量,mealCost代表餐費,tipPercent代表小費占餐費的百分比,taxPercent代表稅率。接下來我們用mealCost、tipPercent和taxPercent這三個變量來計算小費、稅費和總費用。
我們把mealCost乘以tipPercent得到小費的數額,將mealCost乘以taxPercent得到稅費。最終的總費用是餐費、小費和稅費三者之和。
最后,我們使用System.out.println()方法將計算結果打印到屏幕上。