lundi 8 janvier 2018

If loop stops after one iteration

I am trying to make a loop run. Whenever the attempt is 0 or less the program should print out "Error: too low, must allow at least one attempt:". But whenever the attempt is bigger than 0 the loop should stop and the event will be added into the arraylist.

However the loop stops after only one iteration and I don't know how to fix it. I was thinking of making a boolean but I want to know why this doesn't work first before I think of other ideas.

System.out.println("Attempts allowed:");
    attempts = keyboard.nextInt();
    if (attempts <= 0) {
        System.out.println("Error: too low, must allow at least one attempt:");
        keyboard.nextInt();
    }

    if (attempts > 0) {
        eventList.add(new Event(eventName, attempts));
        System.out.println(eventName + " added ");
    }
    }

Aucun commentaire:

Enregistrer un commentaire