Error in a return

0

The return of the root method gives me an error and I do not know what it should be

import java.util.Scanner;

public class RaizCuadrada {
private int numero;

//CONSTRUCTORES

public RaizCuadrada(){
    System.out.println("Inroduce un numero: ");
    Scanner sc = new Scanner(System.in);
    numero=sc.nextInt();
    sc.close();
}

//SET AND GET

public int getNumero() {
    return numero;
}

public void setNumero(int numero) {
    this.numero = numero;
}

//METODOS

public double raiz() {
    double respuesta;
    if(numero > 0) {
        respuesta = Math.sqrt(numero);
    }else {
        System.out.println("El numero introducido es negativo");
    }
    return respuesta;
}

}
    
asked by Vampy95 13.11.2017 в 19:42
source

1 answer

1

IDE Response

  

The response of the local variable may not have been initialized

which gives you to understand that by declaring it without assigning value,

  

double answer;

could generate defects, I recommend you assign -1 to know that if you get that value is because it is "false" you could use it as a flag

public double raiz() {
    double respuesta = -1;
    if(numero > 0) {
        respuesta = Math.sqrt(numero);
    }else {
        System.out.println("El numero introducido es negativo");
    }
    return respuesta;
}
    
answered by 13.11.2017 / 19:50
source