简体   繁体   中英

How do i get my program to count the number of times a method is called?

Ok, I am teaching myself java and I am trying to write a recursive method that can count how many times it is called/used. This is my code so far:

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");
  }

}

This is the output:

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

Every time I run this is prints out final amount but I want it to tell me how many months it took to get there but all it prints out is 1 month. Any help will be greatly appreciated!

**

Completed Code(Thanks to everyone's contributions!)

**

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);
    }
  }
}
  • Zach

Couldn't you just make a global variable like counter outside of the methods? Sort of like this.

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

Just add 1 to counter every time the method is called (within the method you want counted) and it should update the global variable.

Two ways:

1) is to have a variable that exists outside of the method. (If the method lives on an object, you can make it a variable of the object, for example.) At the start of the method, ++variable . Then you can see what value it ends at.

2) is to make the return value, or part of the return value, the number of calls of all of your children. As in:

-If you do not call yourself, return 1.

-If you do call yourself, return the sum of all the return values of all your self-calls (+1 if you also want to count calls of the method part-way through the tree)

Well since you only call Months() once per execution of the application, then counter increments only once! If you want to persist counter between executions of your application, you will have to save the value at the end of the application, and load it in again when you run the application again.

Month() only gets called once in your code, so it sees that counter == 0 , then increments that giving counter == 1 . This is the value being printed.

So, your counter++ is in the wrong place. It should be inside at the top of the Sum() method. As this is the method being recursively called, this is where the counter should increment.

Now when Month() gets called it will have the proper value.

I believe that this should give you what you are looking for.

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));
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM