samedi 26 septembre 2015

Using if/ else statement to check multiple answers?

A few details:

  • I am using Netbeans 8.0
  • All code provided is from a button in a JFrame in my main package.

What my program is suppose to do:

My program consists of a single Jframe that has:

  • 6 TextFields where the user must input his answer
  • 6 Lables to show user what the question is.
  • 1 Button to process everything.
  • 1 TextField for the final mark.

My Programming is a easy math game that helps users develop their multiplication tables.In the first frame there are only six multiplication tables (1 times 1 , 1 times 2 ect...)

So I want to develop if else statements to check ALL of the answers are correct and add them to my final score.

Here's my code for my button so far:

//Declaring my counter
    int TotalCorrect = 0;

    //Declaring my Correct answers
    //To compare to User answers

    int Answer1     =1;
    int Answer2     =2;
    int Answer3     =3;
    int Answer4     =4;
    int Answer5     =5;
    int Answer6     =6;


    // Converting TextField answers to int
    int ConvertedAnswer1 = Integer.parseInt(txfUserAnswer1.getText());
    int ConvertedAnswer2 = Integer.parseInt(txfUserAnswer2.getText());


    if (ConvertedAnswer1 == Answer1)
    {
    //Adding +1 for total correct
    TotalCorrect = + 1;

    }
    else
    {

    TotalCorrect = +0;    

    }

    //Converting int to string

    String FinMark = Integer.toString(TotalCorrect);

    // Printing Users Mark

    txfOutput.setText(FinMark);

The problem is when I use this code directly under the else:

if (ConvertedAnswer2 == Answer2)
    {
    //Adding +1 for total correct
    TotalCorrect = + 1;

    }
    else
    {

    TotalCorrect = +0;    

    }

The program crashes. Not the first or second answer gets marked.

Aucun commentaire:

Enregistrer un commentaire