简体   繁体   中英

How to solve a problem to match the expected output in Java?

The meal before tax and tip is 12.00, the tax percentage of the meal is 20% and the tip of the meal is 8%. You need the use Scanner class to receive input from the user.

12.00
20
8

The expected output is:

15

I tried different ways especially with the code below but I'm getting different result. I can't get 15 as the expected out put.

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

To get the total cost, take the meal cost and add the tip and the tax.

   double  cost = meal_cost + tip + tax;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM