Program that does not end

1

I am doing a program, it is asked to enter 3 data, if the 3 are incorrect the program ends, I do a loop do / while and asks for the 3 data the problem is that the program does not finish and passes to analyze the following condition , how do I end it after entering 3 erroneous data?

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int edad = 0;
    int edad_max = 14;
    int edad_min = 7;
    int id;
    int id_max = 2000;
    int id_min = 1;
    int Intents = 3;
    int i = 0;
    int j=0;
    int intents_totals=0;

    boolean correcto = true;

    do {
        //llegim si es correcte
        System.out.println("digam ID:");
        correcto = scan.hasNextInt();
        if (correcto) {
            id = scan.nextInt();

            if ((id < 1) || (id > 1000)) {
                correcto = false;
                System.err.println("id incorecte, torni a introduirlo: ");
            }

        }

        //System.err.println("el numero te que ser sencer,torni a teclejarlo");
        i++;
    } while ((!correcto)&&(i < 3));
    // aqui deberia finalizar si los 3 intentos hemos introducido dato incorrecto
    // pero salta al siguiente do while

    do {
        i = 0;

        System.out.println("digam l´edad");

        correcto = scan.hasNextInt();
        if (correcto) {
            edad = scan.nextInt();

            if (edad < edad_min || edad > edad_max) {

                correcto = false;
                System.err.println("edad incorrecte, torni a introduirla");

            }
        }
        scan.nextLine();
        i++;
    } while (!correcto && i < Intents);
    
asked by Carlos 07.11.2016 в 18:03
source

4 answers

1

To end your program, you need to make a return; somewhere.

One way to integrate the modification to your program is to modify the condition of the loop a bit, and add another condition inside the loop to end the program if you take too many attempts:

do {
    //llegim si es correcte
    System.out.println("digam ID:");
    correcto = scan.hasNextInt();
    if (correcto) {
        id = scan.nextInt();

        if ((id < 1) || (id > 1000)) {
            correcto = false;
            System.err.println("id incorecte, torni a introduirlo: ");
        }

    }

    i++;

    if (i >= 3) {
        return; // finaliza el programa después de 3 intentos.
    }
} while (!correcto);

Edit

What others say about initializing i outside of your second loop is correct. But that is a different / additional problem in your program that you will have to correct.

Issue 2

If you can not use return; or break; , you can add a condition before your second loop like this:

if (correcto) {
    i = 0;
    do {
        // ...
    } while (...);
}
    
answered by 07.11.2016 / 18:13
source
3

If you want to stop your application you have to perform a validation here:

   do {
        //llegim si es correcte
        System.out.println("digam ID:");
        correcto = scan.hasNextInt();
        if (correcto) {
            id = scan.nextInt();

            if ((id < 1) || (id > 1000)) {
                correcto = false;
                System.err.println("id incorecte, torni a introduirlo: ");
            }

        }

        //System.err.println("el numero te que ser sencer,torni a teclejarlo");
        i++;
    } while ((!correcto)&&(i < Intents));

//Termina el bucle y revisa si i >= Intents y en base a esto termina:

    if (!(i < Intents)){
     return;
    }

In the second loop you must initialize the variable i from your loop, since the problem is that the value of i is always initializing with the value of 0 and the condition to end the loop does not occur :

 i = 0;
 do {
        //i = 0; //INCORRECTO!

        System.out.println("digam l´edad");

        correcto = scan.hasNextInt();
        if (correcto) {
            edad = scan.nextInt();

            if (edad < edad_min || edad > edad_max) {

                correcto = false;
                System.err.println("edad incorrecte, torni a introduirla");

            }
        }
        scan.nextLine();
        i++;
    } while (!correcto && i < Intents); //Si siempre inicializas i = 0 esto no se cumplira!.

If the judgment is always initialized i = 0 i < Intents will always be fulfilled and the loop will not be finished since i would always be less than Intents

    
answered by 07.11.2016 в 18:13
0

The last do/while is always evaluated by setting i=1 . You should remove line i=0 before starting with do/while .

    
answered by 07.11.2016 в 18:14
0

Your logic inside the while is wrong, since you ask that both conditions are met and what you need is that only one of them is fulfilled is to say while correct or i

answered by 07.11.2016 в 18:14