mardi 19 mai 2015

Randomizer Not Assingning Value

public void actionPerformed(ActionEvent e) {
     String inData;
     int tal, slump=0, forsok=0;
     if(e.getSource()==random){
         slump=(int) (Math.random()*100+1);
     }
     //slumpar ett tal mellan 0 och 100
     if(e.getSource()==ok){
         forsok++;
         try{
             inData=tGissa.getText();
             tal = Integer.parseInt(inData);
             if(tal==slump){
                 JOptionPane.showMessageDialog(null, "Det är rätt! "+forsok+" försök.");
             }
             else if(tal<slump && tal>0){
                 JOptionPane.showMessageDialog(null, "För lågt!");
             }
             else if(tal>slump && tal<101){
                 JOptionPane.showMessageDialog(null, "För högt!");
             }
             else{
                 JOptionPane.showMessageDialog(null, "Felaktig inmatning!");
             }
         }
         catch(Exception ex){
             JOptionPane.showMessageDialog(null, "Felaktig inmatning!");
         }
     }
}

You are supposed to guess the correct number between 1 and 100, and the program gives feedback if you are right or wrong and how many tries you have done.

The int "slump" seem to always be 0, even though it should be assigned the value of the randomizer. Any help is needed.




Aucun commentaire:

Enregistrer un commentaire