1

好的,我正在自学java,我正在尝试编写一个递归方法,可以计算它被调用/使用的次数。到目前为止,这是我的代码:

public class FinancialCompany
{
  public static void main(String[] args)
  {
    StdOut.println("Enter your monthly investment: ");
    double money= StdIn.readDouble();
    double total = Sum(money);
    Months();

    StdOut.printf("you have reached an amount of $%8.2f", total);
    StdOut.println();
  }
  public static double Sum(double money)
  {
    double total = (money * 0.01) + money;
    if(total >= 1000000)
    {
      return total;
    }
    else
    {  
      total =(money * 0.01) + money;
      return Sum(total);
    }
  }

  public static int counter = 0;

  public static void Months()
  {
   counter++;
   StdOut.println("It should take about " + counter + " month(s) to reach your goal of $1,000,000");
  }

}

这是输出:

Enter your monthly investment: 1000 (I chose 1000)
It should take about 1 month(s) to reach your goal of $1,000,000
you have reached an amount of $1007754.58

每次我运行它都会打印出最终数量,但我希望它告诉我到达那里需要多少个月,但它打印出来的只是 1 个月。任何帮助将不胜感激!

**

完成代码(感谢大家的贡献!)

**

public class FinancialCompany
{

  public static int counter = 0;

  public static void main(String[] args)
  {
    StdOut.println("Enter your monthly investment: ");
    double money= StdIn.readDouble();
    double investment = money;
    double total = Sum(money, investment);    
    StdOut.println("It should take about " + counter + " month(s) to reach your goal of $1,000,000");
  }
  public static double Sum(double money, double investment)
  {
    counter++;
    double total = (money * 0.01) + money;
    if(total >= 1000000)
    {
      return total;
    }
    else
    {  
      return Sum(total + investment ,investment);
    }
  }
}
  • 扎克
4

5 回答 5

2

你不能只在方法之外创建一个像计数器这样的全局变量吗?有点像这样。

public class testThingy {
    public static int counter = 0;
    /* main method and other methods here */
}

每次调用该方法时(在您想要计数的方法内)只需将 1 添加到计数器,它应该更新全局变量。

于 2013-04-25T23:36:29.537 回答
0

我相信这应该给你你正在寻找的东西。

public static void main(String[] args)
  {
    StdOut.println("Enter your monthly investment: ");
    double money= StdIn.readDouble();
    int months = 0;
    while(money < 10000){
        money += invest(money);
        months++;
    }

    StdOut.println("It should take about " + String.valueOf(months) + " month(s) to reach your goal of $1,000,000");
    StdOut.printf("you have reached an amount of $%8.2f", money);
    StdOut.println();
  }

  private static double invest(double investment){
    return (investment + (investement * 0.01));
  }
于 2013-04-25T23:53:22.703 回答
0

两种方式:

1) 是有一个存在于方法之外的变量。(例如,如果方法存在于对象上,则可以将其设为对象的变量。)在方法的开头,++variable. 然后你可以看到它以什么值结束。

2)是将返回值或部分返回值设置为您所有孩子的调用次数。如:

- 如果您不调用自己,则返回 1。

- 如果您确实调用了自己,则返回所有自调用的所有返回值的总和(如果您还想在树的中途计算方法的调用,则 +1)

于 2013-04-25T23:35:39.490 回答
0

好吧,既然您每次执行应用程序只调用一次 Months(),那么计数器只会增加一次!如果要在应用程序的执行之间保持计数器,则必须在应用程序结束时保存该值,并在再次运行应用程序时再次加载它。

于 2013-04-25T23:35:46.920 回答
0

Month()只在你的代码中被调用一次,所以它会看到counter == 0,然后增加给counter == 1. 这是正在打印的值。

所以,你counter++来错地方了。它应该在Sum()方法的顶部。由于这是递归调用的方法,因此这是计数器应该递增的地方。

现在,当Month()被调用时,它将具有适当的值。

于 2013-04-25T23:41:44.180 回答