Question

This is my code:

static int compoundBalance(double db, double dbTwo, double dbThree) {
if(dbThree == 0) return db;
return (1 + dbTwo)*compoundBalance(db, dbTwo, dbThree-1);
}

And I get these two errors. I'm not sure what to make of them. Any guidance? Thank you.

Factorial.java:60: error: possible loss of precision
    if(dbThree == 0) return db;
                            ^
  required: int
  found:    double

Factorial.java:61: error: possible loss of precision
    return (1 + dbTwo)*compoundBalance(db, dbTwo, dbThree-1);
                      ^
  required: int
  found:    double
2 errors
Was it helpful?

Solution

Your method signature states that you're returning an int when you're actually returning a double. You can fix this by changing your signature to:

static double compoundBalance(double db, double dbTwo, double dbThree) {

This error is to stop you returning something like 6 when you meant to return 6.9. If you really want this behaviour, then instead of changing the signature you can cast the return value as an int.

static int compoundBalance(double db, double dbTwo, double dbThree) {
  if(dbThree == 0) return (int)db;
    return (int)((1 + dbTwo)*compoundBalance(db, dbTwo, dbThree-1));
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top