mercredi 30 janvier 2019

How to make program continue code without printing it more then once

I have this code:

public void checkUserLuckyNumber(PC p, User u) {
    int userLuckyNumber = Integer.parseInt(JOptionPane.showInputDialog(null, "Input lucky number from 1 - 10:"));
    if (userLuckyNumber < 1 || userLuckyNumber > 10) {
        JOptionPane.showMessageDialog(null, Constants.INVALIDINPUTNUMBER);
        System.exit(0);
    }
    for (int i = 1; i <= 3; i++) {
        int threeLuckyNumbers = (int) (Math.random() * 10);

        if (userLuckyNumber == threeLuckyNumbers) {
            JOptionPane.showMessageDialog(null, "you hit a happy number");
        } else {
            JOptionPane.showMessageDialog(null, "you did not hit a lucky number");
        }
    }
}
}

My problem is that my program print me three time message, if user hit lucky number program print me one message "you hit a happy number" and if user miss lucky number, program print me one message "you hit a happy number" and then twice " you did not hit a lucky number".

So my question is how to make program that print just one message.

Aucun commentaire:

Enregistrer un commentaire