vendredi 26 mai 2017

Create Random number in java

I need to program a code that gives the user an equation if the user solves it a promo code will be given.

1, I need to make the random number from 1 to 9 only.

2, No "random" number displayed in JOptionpane:(!

3, I also need the promo code to be a random number too that contains more than one digits (4 digits are good).

Thanks in advance!

    private void promoCodeButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                
    Random num1 = new Random();
    Random num2 = new Random();
    Random num3 = new Random();

    //convert Random to String so I use it with JOptionPane
    String n1=num1.toString();
    String n2=num2.toString();
    String n3=num3.toString();

    String equation = "What's the result of "+n1+"+"+n2+"*"+n3+"=?";
    String given = JOptionPane.showInputDialog(null, equation,connection);

    //convert String to int so I can apply equation:
    int numb1 = Integer.parseInt(n1);
    int numb2 = Integer.parseInt(n2);
    int numb3 = Integer.parseInt(n3);
    int result = numb1+numb2*numb3;
    int result1 =Integer.parseInt(given);

    //Random number promo code
    Random promo_code = new Random();

    if(given.equals(result1)){
    JOptionPane.showMessageDialog(rootPane, "Correct Answer! \nHere's your promo code is:"+promo_code+"\nEnjoy! ");
    }

    else{
    JOptionPane.showMessageDialog(rootPane, "Wrong Answer! \nTry again");

    }
}                                             




Aucun commentaire:

Enregistrer un commentaire