Failed to return False or true Java

1

I have a logic error that is giving me headaches, I need to make a login system, the fields are burned, but I need to pass it to a method, when I execute it it does not return me true or false , and executing it as a method does not realize the count of the three attempts .

How could I improve it?

public class Login1 {

    public static boolean login(boolean login) {

        Scanner teclado = new Scanner(System.in);
        int cont;
        boolean salir = false;
       String user[] = {"Usuario1", "Usuario2", "Usuario3"};
        String usuario, contra;
        String pass[] = {"uno1", "Dos2", "tRes3"};

        System.out.println("Ingrese su usuario y contraseña para iniciar");

        while (!salir) {
            for (int i = 0; i < user.length; i++) {
                int contInt = 1;
                int a = i;
                System.out.println("3[30mIntento " + (a + 1));
                System.out.println("Digite su usuario: ");
                usuario = teclado.nextLine();
                System.out.println("Digite su contraseña: ");
                contra = teclado.nextLine();
                if (usuario.equals(user[0]) && (contra.equals(pass[0])) || usuario.equals(user[1]) && (contra.equals(pass[1])) || usuario.equals(user[2]) && (contra.equals(pass[2]))) {

                    System.out.println("Bienvenido al sistema");
                    login=true;


                    salir = true;
                    //contInt++;
                } else {
                    System.out.println(" ");
                    System.out.println("3[31m***********************************");
                    System.out.println("3[31m* USUARIO O CONTRASEÑA INCORRECTA *");
                    System.out.println("3[31m***********************************");
                    System.out.println(" ");
                    if (i == 2) {
                        System.err.println("HA CUMPLIDO CON LA CANTIDAD DE INTENTOS PERMITIDOS.");
                        System.err.println("SISTEMA BLOQUEADO, ESPERE... ");
                        System.out.println(" ");
                        try {
                            Thread.sleep(10000); // espera en milisegundos
                        } catch (Exception e) {
                            login=false;
                        }

                    }
                    salir = false;

                }

            }
        }//fin wh
        return login;
    }

    public static void main(String[] args) {
        boolean iniciar = true;
        Login1.login(iniciar);
        System.out.println(login(iniciar));
    }
}
    
asked by Felix mejias 14.08.2018 в 06:41
source

1 answer

0

As I commented, I do not see the utility of passing a value booleano to a function and returning that value again. (unless some detail is happening to me)

If you use fixed indexes such as (user [0] to specify the position of an array (0) , there is no point in having a for that iterates over the array of elements.

To have and iterate over the elements and if you want to add more users and passwords, you must use the for and access the element to be validated in both the user and passwords array, making use of the index i , also have a new variable booleana (login) , to handle when you must exit the cycle for .

public static void login() {
    Scanner teclado = new Scanner(System.in);
    boolean salir =  false;
    login=false;
    String user[] = {"Usuario1", "Usuario2", "Usuario3","admin"};
    String usuario, contra; 
    String pass[] = {"uno1", "Dos2", "tRes3","admin"};
    int contInt =1;
    while (!salir) {
            System.out.println("3[30mIntento " + contInt);
            System.out.println("Ingrese su usuario y contraseña para iniciar");
            System.out.println("Digite su usuario: ");
            usuario = teclado.nextLine();
            System.out.println("Digite su contraseña: ");
            contra = teclado.nextLine();
        for (int i = 0; i < user.length && !login; i++) { //mientras no haya login
            if (usuario.equals(user[i]) && (contra.equals(pass[i]))) {
                // cuando coincidan los valores, se corta el ciclo for
                // mediante la variable booleana login 
                login= true; 
            }
        }
        if(login){ // Si se ingresaron los datos correctos
           System.out.println("Bienvenido al sistema"); 
           salir = true; //salimos del while
        }
        else{
            System.out.println(" ");
            System.out.println("3[31m***********************************");
            System.out.println("3[31m* USUARIO O CONTRASEÑA INCORRECTA *");
            System.out.println("3[31m***********************************");
            System.out.println(" ");
            if (contInt == 3) {
                System.err.println("HA CUMPLIDO CON LA CANTIDAD DE INTENTOS PERMITIDOS.");
                System.err.println("SISTEMA BLOQUEADO, ESPERE... ");
                System.out.println(" ");
                try {
                    Thread.sleep(10000); // espera 10 segundos, en milisegundos
                    contInt=1; //reseteamos el contador de intentos

                } catch (Exception e) {
                }

            }             
            contInt ++; //añadimos el contador de intentos
        }
    }
}

DEMO

    
answered by 14.08.2018 в 07:30