Here’s the problem:
double math = 1/k;
and
formula2 -= 1/k;
k
is an int
variable, so the JVM won’t never return a decimal number in this statement. It will take only two possible values: 0 (if k > 1) or 1 (if k == 1) because the JVM performs the division before promoting the result to double
.
Try this:
formula2 -= 1/(double)k;
Take a look at Numeric Promotions
solved Why does my code return 1 always?