Search code examples
javajava.util.scanneruser-input

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

Solution

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

       double  cost = meal_cost + tip + tax;