gpt4 book ai didi

java - 得到错误的餐费

转载 作者:行者123 更新时间:2023-11-30 08:33:02 26 4
gpt4 key购买 nike

****

给定餐费(一顿饭的基本成本)、小费百分比(作为小费添加的餐费百分比)和餐费百分比(作为税添加的餐费百分比),找到并打印这顿饭的总费用。

这是我的代码

public class Solution3 {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double mealCost = scan.nextDouble(); // original meal price
int tipPercent = scan.nextInt(); // tip percentage
int taxPercent = scan.nextInt(); // tax percentage
double tip = mealCost *(tipPercent/100);
double tax = mealCost*(taxPercent/100);
double total= mealCost+tip+tax;
// cast the result of the rounding operation to an int and save it as totalCost
int totalCost = (int) Math.round(total);

System.out.println("The total meal cost is "+totalCost+" dollars");
}
}

预期输出

The total meal cost is 15 dollars.

我的输出

The total meal cost is 12 dollars.

请帮帮我。

如有任何帮助,我们将不胜感激。

最佳答案

这是一个整数除法问题。

试试这个:

double tip = mealCost *(tipPercent/100.0);
double tax = mealCost*(taxPercent/100.0);

关于java - 得到错误的餐费,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39652409/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com