Java:如何在递归调用中使用原始参数递归调用我的方法

玛姬S.

我有一种方法可以使用ArrayQueue实现来计算股票市场程序的资本损益。

但是我需要递归调用此方法,以便正确计算资本。

示例:队列中有3个采购交易(对象)。100股每股10美元,150股每股20美元和50股每股30美元。但是现在我只想出售275股,每股价格为25美元。

这是我计算资本的方法:

public static int calculateCapital(Transaction sale, 
                                   Transaction purchase, ArrayQueue q) {

    int sharesRemaining = 0;
    int capital = 0;

    //if shares to be sold is less that the shares in the next transaction
    if (sale.getShares() < purchase.getShares()) {

        //calculate the capital
        capital = capital + (sale.getShares()*sale.getPrice())-
                            (sale.getShares()*purchase.getPrice());

        // return the remaining shares to the queue
        purchase.setShares(purchase.getShares()-sale.getShares()); 
        q.enqueue(purchase);

    //if shares to be sold are more than the shares in the next transaction
    } else if (sale.getShares() > purchase.getShares()) {

       //calculate the capital
       capital = capital + (sale.getShares()*sale.getPrice())-
                           (sale.getShares()*purchase.getPrice());

       //store the remaining share count needed to be sold
       sharesRemaining = sale.getShares() - purchase.getShares();

       sale.setShares(sharesRemaining);

       Transaction nextPurchase = (Transaction)q.dequeue();

       while (sharesRemaining > 0) {
          // RECURSIVELY CALL CALCULATECAPITAL METHOD
          calculateCapital(sale, nextPurchase, q);
       }         

    //if shares to be sold are equal to the shares in the next transaction              
    } else if (sale.getShares() == purchase.getShares()) {

       //calculate the capital
       capital = capital + (sale.getShares()*sale.getPrice())-
                           (sale.getShares()*purchase.getPrice());

    }

    return capital;

}

当我运行客户端测试程序时,我收到一个错误消息,说当队列去下一个事务时,该队列为空,这是因为我没有将相同的队列传递给递归调用,而是将其传递给了空队列。

有没有办法将输入到原始方法调用中的相同Queue传递到递归方法中?喜欢calculateCapital(sale, nextPurchase, this.q);吗?

双锐

您的代码有两个问题-最大的问题是您不想使用while循环来进行递归方法调用。sharesRemaining例如,如果为10,它将永远保持不变(它是局部变量)。您要做的是使用一条if语句并添加方法调用的结果。q.dequeue();在检查是否还有任何其他内容之前,您还需要先调用,然后sharesRemaining将其移到if语句中。

   // more code above this, but...

   /* you do not want to use sale.getShares() as this is a decrementing value 
      as the method is called recursively. you want to know the purchase price
      for the number of shares in this transaction using the purchase price and
      sale price. use a double instead of int to calculate cents */
   double salePrice = purchase.getShares() * sale.getPrice();
   double purchasePrice = purchase.getShares() * purchase.getPrice();

   // calculate the capital, only for this transaction
   double capital = salePrice - purchasePrice;

   // store the remaining share count needed to be sold, this is passed into the method
   // and why it should not be used to calculate the capital
   sale.setShares(sale.getShares() - purchase.getShares());

   // If we have more shares, call the method to process the next one.
   if (sale.getShares() > 0) {
      // Get the next transaction
      Transaction nextPurchase = (Transaction)q.dequeue();

      // Add this value to the value of the "next" transaction, recursively
      capital += calculateCapital(sale, nextPurchase, q);
   }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在参数中使用单引号来递归系统bash调用

来自分类Dev

.NET C#-如何在递归调用中使用变量泛型类型?

来自分类Dev

如何在Lua中使用递归函数动态建表而不用每次调用都覆盖它

来自分类Dev

如何在C ++中的类中递归调用函数方法?

来自分类Dev

如何在打字稿中递归调用方法

来自分类Dev

如何在Swift中使用CVaListPointer参数调用方法

来自分类Dev

如何在Android Studio中使用View参数调用方法

来自分类Dev

避免在递归调用中使用null ref

来自分类Dev

我在Java中递归调用的StackOverflowError

来自分类Dev

如何在PHP中使用其原始参数调用父类构造函数

来自分类Dev

如何让我的爬虫进行递归调用?

来自分类Dev

如何在递归调用中保持值?

来自分类Dev

如何在javascript中调用递归函数

来自分类Dev

Java的:很难理解递归方法调用

来自分类Dev

如何在JavaScript中使用变量跟踪值,而不在递归调用中重置变量而不使用全局变量?

来自分类Dev

使用ActionListener调用递归树方法

来自分类Dev

Haskell,递归调用,许多参数

来自分类Dev

如何在Java中使用csv文件调用方法?

来自分类Dev

如何在循环中使用递归方法退出循环?

来自分类Dev

如何在Java中使用递归获取goldenRatio?

来自分类Dev

如何在Java中使用递归对单词“ hello”进行排序?

来自分类Dev

在表达式主体成员中使用重新分配的参数进行递归调用

来自分类Dev

如何使用反射来调用与原始参数的方法?

来自分类Dev

Java递归-几个递归调用的输出

来自分类Dev

递归调用使我感到困惑

来自分类Dev

递归调用使我感到困惑

来自分类Dev

如何在调用 API 时以递归方式向参数发送输入

来自分类Dev

如何在Hive中使用递归查询

来自分类Dev

如何在std :: list中使用递归?

Related 相关文章

  1. 1

    在参数中使用单引号来递归系统bash调用

  2. 2

    .NET C#-如何在递归调用中使用变量泛型类型?

  3. 3

    如何在Lua中使用递归函数动态建表而不用每次调用都覆盖它

  4. 4

    如何在C ++中的类中递归调用函数方法?

  5. 5

    如何在打字稿中递归调用方法

  6. 6

    如何在Swift中使用CVaListPointer参数调用方法

  7. 7

    如何在Android Studio中使用View参数调用方法

  8. 8

    避免在递归调用中使用null ref

  9. 9

    我在Java中递归调用的StackOverflowError

  10. 10

    如何在PHP中使用其原始参数调用父类构造函数

  11. 11

    如何让我的爬虫进行递归调用?

  12. 12

    如何在递归调用中保持值?

  13. 13

    如何在javascript中调用递归函数

  14. 14

    Java的:很难理解递归方法调用

  15. 15

    如何在JavaScript中使用变量跟踪值,而不在递归调用中重置变量而不使用全局变量?

  16. 16

    使用ActionListener调用递归树方法

  17. 17

    Haskell,递归调用,许多参数

  18. 18

    如何在Java中使用csv文件调用方法?

  19. 19

    如何在循环中使用递归方法退出循环?

  20. 20

    如何在Java中使用递归获取goldenRatio?

  21. 21

    如何在Java中使用递归对单词“ hello”进行排序?

  22. 22

    在表达式主体成员中使用重新分配的参数进行递归调用

  23. 23

    如何使用反射来调用与原始参数的方法?

  24. 24

    Java递归-几个递归调用的输出

  25. 25

    递归调用使我感到困惑

  26. 26

    递归调用使我感到困惑

  27. 27

    如何在调用 API 时以递归方式向参数发送输入

  28. 28

    如何在Hive中使用递归查询

  29. 29

    如何在std :: list中使用递归?

热门标签

归档