[英]How to solve a problem to match the expected output in Java?
稅前餐費和小費為12.00,餐費稅率為20%,餐費小費為8%。 您需要使用掃描儀 class 來接收用戶的輸入。
12.00
20
8
預期的 output 為:
15
我嘗試了不同的方法,特別是使用下面的代碼,但我得到了不同的結果。 我不能像預期的那樣得到 15 。
enter public class MealSolution {
private static final Scanner scanner = new Scanner(System.in);
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.print("Enter cost of meal: ");
double meal_cost = scanner.nextDouble();
System.out.print("Enter the tip percent: ");
int tip_percent = scanner.nextInt();
System.out.print("Enter tax of meal: ");
int tax_percent = scanner.nextInt();
double tip = meal_cost * tip_percent/100;
double tax = meal_cost * tax_percent/100;
double cost = meal_cost + tip + tax;
long total_cost = Math.round(cost);
System.out.println(total_cost);
scanner.close();
}
}
要獲得總成本,請計算餐費並加上小費和稅金。
double cost = meal_cost + tip + tax;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.