samedi 13 juin 2015

Create algebraic equation and ask user to answer

I've created a random question generator. Each question is in the form: x=3/random number. The problem is that the program never recognizes the fact that the user's answer is correct. I've made it print out the answer and copy it into the answer input (text pane), yet it always prints out "wrong."

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

//Generates random value for n (2 decimal plcae)   
double nMin = 1.0;//minimum
double nMax = 38.6;//maximum
Random rn = new Random();  
double nRand = nMin + (nMax - nMin) * rn.nextDouble();

//Calculates corresponding value of v
String x = String.format("%.2f", 3/nRand );
double nAns = Double.parseDouble(x);//corresponding value of c
check.setText(x);

//dsiplays question
question.setText("n = " + String.format( "%.2f", nRand ) + "     v = ?");//question
String answer = answerInput.getText();
Double nUserA = Double.parseDouble(answer);
//checks user's answer
if(x == answer)
{
    check.setText("correct");
}    
if(nUserA != nAns)
    check.setText("wrong");  


}                                      




Aucun commentaire:

Enregistrer un commentaire