【问题标题】:Java method that multiplies a number x times by a number that increments x timesJava 方法将一个数字乘以 x 次递增的数字
【发布时间】:2021-04-17 17:42:03
【问题描述】:

Example of the desired outcome

大家好!我对 Java 还很陌生,我在创建一个应该计算每月税收贡献的函数时遇到了问题。

我有一个:

  • 月薪(年薪除以14-年薪金额)
  • int number n,表示月薪除以 200 的倍数

我需要创建一个函数,将月薪乘以另一个变量,我们称之为“税”,它从 0 开始,然后递增 0.01“n”次

我附上了一张期望结果的照片,可以更好地解释上下文。

谁能帮我理解如何在一个 double 上迭代 n 次并在每次迭代迭代时将其增加 n 次?

我在这里的迭代卡住了:

public default double pensionContribution() {
        double monthSalary = (calculateSalary() / 14);
        double n = Math.round(monthSalary / 200);
        tax = 0;
        for(int i = 0; i < n; i++) {
            tax= (n/100);
    
    }

enter image description here.stack.imgur.com/UL4cO.jpg

【问题讨论】:

    标签: java methods iteration


    【解决方案1】:

    为什么monthSalary除以14??

    我不知道你想那样做

    public default double pensionContribution() {
        double monthSalary = (calculateSalary() / 14);
        double n = Math.round(monthSalary / 200);
        double sum = 0;
        tax = 0;
        for(int i = 0; i < n; i++) {
        sum = monthSary + monthSarly * tax;
        tax = tax + 0.01;
    
    }
    

    【讨论】:

      【解决方案2】:

      据我所知,这就是您所需要的。将整数除法转换为双一。

      public default double pensionContribution() {
        double monthSalary = (calculateSalary()/14);
        double n = Math.round(monthSalary/200);
        double sum= 0.0;
       
        // start loop and calculate total tax for n months
        for (int i = 1; i <= n; i++) { 
          sum += monthSalary * (n*1.0)/100; // you required to convert integer division into double
        }
      
        return sum;
      }
      

      【讨论】:

        猜你喜欢
        • 2013-12-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-19
        • 2018-05-09
        • 2019-06-28
        • 1970-01-01
        • 2019-10-17
        相关资源
        最近更新 更多