Home > other >  Why the if method it's ignored or doesn't work? [closed]
Why the if method it's ignored or doesn't work? [closed]

Time:10-08

I want to calculate how much money would someone save in the end of one year, each month that person does a bank deposit, but if the deposit is greater than the amount of money they have at that moment, they get a bonus of 1% of the deposit of the month, if it's not greater, then the bonus doesn't get added, the problem is that the bonus does not get added to the money saved, even when i specify to do that

    float ahorroActual;
    float depositosMensuales;
    int conteoMeses=1;
    int acumulador=1;
    float sumaAhorros=0;
    float ahorroMes;
    float ahorroBono;
    float ahorroConBono;
    
while (conteoMeses<13){
    System.out.println("Ingrese el deposito del mes " ((int)conteoMeses));
    depositosMensuales=leer.nextFloat();
    float totalAhorrado = sumaAhorros =depositosMensuales;
    
    //This is where the bonus is supposed to get added, but for some reason, it doesn't
    if (depositosMensuales>totalAhorrado){
        ahorroConBono=0.01f*depositosMensuales;
        ahorroBono=totalAhorrado ahorroConBono;
        System.out.println("Su ahorro en el mes "   conteoMeses   " con bono añadido es de "  ahorroBono);
    } else{
            System.out.println("Su ahorro en el mes "   conteoMeses   " es de "  totalAhorrado);
            }
            
        conteoMeses=conteoMeses acumulador;
    }
}

}

CodePudding user response:

you must compare

 depositosMensuales=leer.nextFloat();
float totalAhorrado = sumaAhorros =depositosMensuales;

//This is where the bonus is supposed to get added, but for some reason, it doesn't
if (depositosMensuales>sumaAhorros){

CodePudding user response:

It seems like wrong calculation, if the deposit is greater than the amount of money they have at that moment it means before depositing the money, we should compare first.

float totalAhorrado=sumaAhorros;
sumaAhorros  = depositosMensuales;
if (depositosMensuales>totalAhorrado){
  ahorroConBono=0.01f*depositosMensuales;
  totalAhorrado=ahorroConBono totalAhorrado depositosMensuales;
  System.out.println("Su ahorro en el mes "   conteoMeses   " con bono añadido es de "  totalAhorrado);
} 
else{
  totalAhorrado=totalAhorrado depositosMensuales
  System.out.println("Su ahorro en el mes "   conteoMeses   " es de "  totalAhorrado);
}
  • Related