this is my code:
import java.text.NumberFormat;
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
float yearlyReturn = 200f;
int totalGain = 0;
int monthlyPayOut = 100000;
int yearsWaiting = 8;
int firstMoney = 0;
float returnGet = (1 + yearlyReturn/100);
totalGain += firstMoney;
for (int i = 0; i < yearsWaiting; i++){
totalGain += (monthlyPayOut * 12);
totalGain *= returnGet;
System.out.println(totalGain);
if (totalGain < 0){
totalGain = Math.abs(totalGain);
System.out.println(totalGain);
}
}
}
}
It works normally until 8th loop, and it gives a negative number. I tried to change it into positive with Math.abs but it doesn't work.
first I want to know why it gives negative number?!
and second how does Math.abs work?!