如何在Java中解决匹配预期输出的问题?

huangapple go评论108阅读模式
英文:

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 to use the 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 a different result. I can't get 15 as the expected output.

public class MealSolution {

    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        
        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();
    }
}

The code you provided seems correct. It should calculate the total cost correctly based on the input values you mentioned (12.00, 20, 8) and print 15 as the output. If you are getting a different result, please double-check that you are entering the input values correctly, and there are no syntax errors or issues with your Java environment.

英文:

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

答案1

得分: 1

获取总费用,取餐费用并加上小费和税费。

double cost = meal_cost + tip + tax;
英文:

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

   double  cost = meal_cost + tip + tax;

huangapple
  • 本文由 发表于 2020年8月2日 10:29:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63211810.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定