error in bracket

0

Good morning family, I'm very green in java and I've been trying to solve a very silly mistake for hours and I do not hit the key. I get an error by a bracket, to see if you see the failure and you tell me, Thanks in advance.

public double retirar(){
            if(saldo<= 0){
          System.out.println("no hay saldo suficiente");
          return this.saldo;
           }
            else if (saldo>=cantidad){ 
           System.out.println("retirar dinero" +cantidad);
        return this.cantidad;                   
            }

} - in this bracket it gives me the error ...

    
asked by Rafael Montero 20.01.2018 в 11:58
source

2 answers

0

The problem is that you need to return a value when the balance is between 0 and quantity. On the other hand, you could improve the readability of the code by correctly identifying it. Although in Java it is not mandatory (in Python, for example, if) if it is convenient. I enclose my proposal. Look to see if it works for you, changing XXX for the value to be returned in case the balance is between 0 and quantity.

public double retirar(){
        if(saldo<= 0){
            System.out.println("no hay saldo suficiente");
            return this.saldo;
        }
        else if (saldo>=cantidad){ 
            System.out.println("retirar dinero" +cantidad);
            return this.cantidad;                   
        }
        else{
            return XXX;//lo que tenga que devolver cuando saldo> 0 y saldo < cantidad
        }
    }
    
answered by 20.01.2018 в 12:37
0

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
    
answered by 20.01.2018 в 13:16