【问题标题】:Calculate sales tax according to the price根据价格计算销售税
【发布时间】:2020-02-06 14:30:25
【问题描述】:

我正在尝试做java作业,场景如下:

对所有消费的商品和服务征收 7% 的销售税。所有价格标签都必须包含销售税。例如,如果一件商品的标价为 107 美元,则实际价格为 100 美元,其中 7 美元用于缴纳销售税。

编写程序,使用循环连续输入含税价格(如“double”);计算实际价格和销售税(“双”);并打印四舍五入到小数点后 2 位的结果。程序将响应输入 -1 终止;并打印总价、总实际价格和总销售税。

但是,当我尝试计算销售税时,而不是显示:

输入·本·含税·价格·in·美元·(或·-1·到·结束):107

实际·价格·是:$100.00

销售额·税收·是:$7.00

我的计算表明:

以美元为单位输入含税价格(或 -1 结束):107

实际价格为 99.51 美元

销售税为:7.49 美元

我不确定我的编码有什么问题。

import java.util.Scanner;
public class SalesTax{

  public static void main(String[] args) {



// Declare constants
      final double SALES_TAX_RATE = 0.07;
  final int SENTINEL = -1;        // Terminating value for input

  // Declare variables
  double price, actualPrice, salesTax;  // inputs and results
  double totalPrice = 0.0, totalActualPrice = 0.0, totalSalesTax = 0.0;  // to accumulate


  // Read the first input to "seed" the while loop
  Scanner in = new Scanner(System.in);
  System.out.print("Enter the tax-inclusive price in dollars (or -1 to end): ");
  price =  in.nextDouble();

  while (price != SENTINEL) {
     // Compute the tax

     salesTax = SALES_TAX_RATE * price;
      actualPrice = price - salesTax;


     // Accumulate into the totals
     totalPrice = actualPrice + salesTax;
     totalActualPrice = actualPrice + actualPrice; 
     totalSalesTax = salesTax + salesTax;
     // Print results
     System.out.println("Actual price is $" + String.format("%.2f",actualPrice));
     System.out.println("Sales Tax is: $" + String.format("%.2f",salesTax));


     // Read the next input
     System.out.print("Enter the tax-inclusive price in dollars (or -1 to end): ");
     price =  in.nextDouble();
     // Repeat the loop body, only if the input is not the sentinel value.
     // Take note that you need to repeat these two statements inside/outside the loop!
  }
  // print totals
  System.out.println("Total price is: " + String.format("%.2f",totalPrice));
  System.out.println("Total Actual Price is: " + String.format("%.2f",totalActualPrice));
  System.out.println("Total sales tax is: " + String.format("%.2f",totalSalesTax));




  }
}

任何帮助将不胜感激。谢谢!

【问题讨论】:

    标签: java


    【解决方案1】:

    这两行你错了:

    salesTax = SALES_TAX_RATE * price;
    actualPrice = price - salesTax;
    

    您正在计算已售出的销售额,请关注我:

    您的priceactualPriceactualPrice 上的税金的组合:

    price = actualPrice + SALES_TAX_RATE * actualPrice
    

    所以你可以在数学上做以下段落:

    price = actualPrice * (1 + SALES_TAX_RATE)
    
    actualPrice = price / (1 + SALES_TAX_RATE)
    

    所以,尝试更改actualPrice 的分配,然后计算actualPrice 的税:

    salesTax = SALES_TAX_RATE * actualPrice;
    

    【讨论】:

      【解决方案2】:

      您应该先计算实际价格,然后再计算销售税率 而不是使用这个 salesTax = SALES_TAX_RATE * price; actualPrice = price - salesTax;

      使用这个计算: actual_price = price / 1.07; tax_price = price - actual_price;

      1.07 来自于将税收百分比除以 100 并在其上加 1(您可以对任何数量的百分比进行此操作)

      【讨论】:

        【解决方案3】:

        我遇到了类似的事情并使用了这个逻辑。您需要将销售税除以 100,例如 7/100,其中 7 是税收变量的百分比,或者您可以乘以 0.01,然后将其结果添加到变量的总值中。 1% 是 100% 的 0.01。所以 7% 将是 0.07 乘以您的变量并添加到您尝试计算的总数中。如果你想要精度而不是四舍五入到最接近的整数,那么只需在 Math.round 函数上设置参数,或者根本不使用它。

        public static void solve(double meal_cost, int tip_percent, int tax_percent) {
            double tip = Math.round((tip_percent * .01) * meal_cost);
            double tax = Math.round((tax_percent * .01) * meal_cost);
            int total_cost = (int)(meal_cost + tip + tax);
            System.out.println(total_cost);
        }
        

        【讨论】:

        • 请重新阅读问题。您正在做与 OP 在其功能中所做的完全相同的错误。 OP 不是在问如何加税,而是在问如何提取税。
        【解决方案4】:

        问题是你的销售税计算,试试:

        final double SALES_TAX_RATE = 7;
        
        salesTax = price / (100 + SALES_TAX_RATE) * SALES_TAX_RATE;
        

        【讨论】:

        • "107" 是一个例子,你应该解释一下如何解决他的问题;但是,在他的示例中,“价格 / 107”等于 1,它仅适用于我们处于特殊情况:您的答案不正确或不完整
        • 价格为实际价格的107%。将价格除以 107 得到 1% 的价格,乘以 7 得到 7% 的销售税。虽然我应该使用变量来表示税收百分比,而不是将其限制为 7%。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-07
        • 1970-01-01
        相关资源
        最近更新 更多