How to have a sentence repeated until it is correct

0

I would like to know how I can make the code repeat until it is correct, in this case greater than 400hrs

/**
 * Metodo para obtener las horas de vuelo del piloto, con validacion de mayor a 400.
 */
public int HorasVuelo(){
    Scanner sc = new Scanner(System.in);
    int fakeHoraVuelo;
    System.out.println("Ingrese las horas de vuelo del piloto");
    fakeHoraVuelo = sc.nextInt();
    if(fakeHoraVuelo < 400){
        System.out.println("Lo sentimos, se necesita un Piloto con mas experiencia");
    }
    else{
        System.out.println("Esplendido!");
        HorasDeVuelo = fakeHoraVuelo;
    }
return HorasDeVuelo;   
}

I mean, if you enter for example 300, tell you the message and return to the beginning to ask for flight times again

    
asked by unknownnacho 10.12.2017 в 05:11
source

2 answers

1

Here is a functional answer from your code:

public int HorasVuelo(){
    int fakeHoraVuelo = 0;
    int horasDeVuelo = 0;
    Scanner sc = new Scanner(System.in);

    while(fakeHoraVuelo < 400){
        System.out.println("Ingrese las horas de vuelo del piloto");
        fakeHoraVuelo = sc.nextInt();



        if(fakeHoraVuelo < 400){
            System.out.println("Lo sentimos, se necesita un Piloto con mas experiencia");
        }
        else{
            System.out.println("Esplendido!");
            horasDeVuelo = fakeHoraVuelo;
        }

    }

return horasDeVuelo;   
}
    
answered by 10.12.2017 в 14:30
1

Have you tried a do-while cycle?

Functional example from your code:

do{
    System.out.println("Ingrese las horas de vuelo del piloto");
    fakeHoraVuelo = sc.nextInt();

    if(fakeHoraVuelo < 400) // Validar número y dar feedback
        System.out.println("Lo sentimos, se necesita un Piloto con mas experiencia");

    else {
        System.out.println("Esplendido!");
        HorasDeVuelo = fakeHoraVuelo;
       }
} while (fakeHoraVuelo < 400); // Condición

return HorasDeVuelo;
    
answered by 10.12.2017 в 15:50