首页 > 解决方案 > 如何解决问题以匹配 Java 中的预期输出?

问题描述

税前餐费和小费为12.00,餐费税率为20%,餐费为8%。您需要使用 Scanner 类来接收来自用户的输入。

12.00
20
8

预期的输出是:

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();
    }
}

标签: javajava.util.scanneruser-input

解决方案


要获得总成本,请计算餐费并加上小费和税金。

   double  cost = meal_cost + tip + tax;

推荐阅读