The problem is not the bracket itself.
You have basically two problems:
- The variable
cantidad
must be passed in parameter to the method, to know how much money you have to withdraw. It is assumed that the variable saldo
is within the scope of the method, so we do not mess with that.
- There is a logic error. The method must evaluate at least: that the balance is not
0
or negative; that the amount to be withdrawn is not greater than the balance; that the amount to be removed is not zero or negative.
- Also, you can store the final result in a variable, and make a single
return
when you exit the conditional blocks.
See this code as an example.
We will test the method by passing it three different values.
VIEW DEMO IN REXTESTER
class Rextester
{
private static double dblSaldo=7000.00;
public static void main(String args[])
{
/*Probando el método con tres valores distintos*/
retirar(420.99);
retirar(100000.30);
retirar(0);
}
public static double retirar(double dblCantidad){
double dblNuevoSaldo=dblSaldo;
if(dblSaldo<=0){
System.out.println("no hay saldo suficiente");
}else if(dblCantidad<=0){
System.out.println("Nada que retirar: "+dblCantidad);
}else if(dblSaldo>=dblCantidad){
dblNuevoSaldo=dblSaldo-dblCantidad;
System.out.println("Se ha retirado: " + String.format("%,.2f",dblCantidad)+ " El nuevo saldo es: " +String.format("%,.2f",dblNuevoSaldo));
}else{
System.out.println("No puede ser. Quieres retirar: " + String.format("%,.2f", dblCantidad)+ " Pero tu saldo es: " +String.format("%,.2f",dblSaldo));
}
return dblNuevoSaldo;
}
}
Test result:
Se ha retirado: 420.99 El nuevo saldo es: 6,579.01
No puede ser. Quieres retirar: 100,000.30 Pero tu saldo es: 7,000.00
Nada que retirar. Pediste que se retire: 0.0