Fill array by keyboard within a [closed] range

-1

I can not demand an entry range when filling an array, I show you:

 public static void llenar(int []sueldos, String [] nombres){

            for(int i = 0; i<nombres.length; i++){
                imprimir("Ingrese el nombre del empleado numero " + (i+1) + " : ");
                    nombres[i]=lectura.next();


                        imprimir("Ingrese el sueldo de " + (nombres[i]) + " : ");
                            sueldos[i]= lectura.nextInt();
                            if(sueldos[i]<100|| sueldos[i] >500){
                                    imprimir("El empleados no esta asegurado \n");

                            }
                                imprimir("\n"); 
            }                     
       }

As you can see when the salary entered is less than 100 or more than 500 I show the message that the employee is not insured, but I store it in the array as well, what I would really like to do is create a repetitive structure inside the loop to prevent him from entering the salary unless it was within that range, thanks in advance.

    
asked by Rafael Valls 29.10.2017 в 17:01
source

3 answers

1

You can use a while cycle

imprimir("Ingrese el sueldo de " + (nombres[i]) + " : ");
sueldos[i]= lectura.nextInt();
while(sueldos[i]<100 || sueldos[i] >500) {
    imprimir("El empleados no esta asegurado \n");
    sueldos[i] = lectura.nextInt()
}

This would continue to ask for the salary until it is correctly entered and it would be kept in the same position. I hope I help you

    
answered by 29.10.2017 / 17:53
source
1

Here is an example I made for you

    String nombres[] = new String[3];
    int sueldos[] = new int[3];
    Scanner lectura = new Scanner(System.in);

    for(int i = 0; i<nombres.length; i++){
        System.out.println("Ingrese el nombre del empleado numero " + (i+1) + " : ");
            nombres[i]=lectura.next();

                    do{
                        System.out.println("Ingrese el sueldo de " + (nombres[i]) + " : ");
                        sueldos[i]= lectura.nextInt();

                        if(sueldos[i] > 100 && sueldos[i] < 500){
                            System.out.println("El empleados no esta asegurado \n");
                        }

                    }while(sueldos[i] > 100 && sueldos[i] < 500);

                    System.out.println("\n");
    }  
    
answered by 29.10.2017 в 17:54
0

Because you do not do a boolean type function comparing the ingested salary with the rank, according to the value returned you save it or not

private boolean sueldoEnRango (double sueldo) {
    boolean estaEnRango=false;
    if (sueldo>100 && sueldo <500){
        estaEnRango=true;
    }
    return estaEnRango;
}

Then you would do your main

for( int i=O ; .....) {
    // ingresará por teclado el sueldo 
    ....
    if (sueldoEnRango==true){
        sueldo[i] = sueldo_ingresado;
    }
}
    
answered by 29.10.2017 в 17:27