For my java trivia game, I set the questions in the beginning, e.g.
if(command.equals("question 1"))
{
txtAnswer.setText("");
q1 = true;
btnQ1.setEnabled(false);
lblQuestion.setText("What is 2 + 2?");
lblOutcome.setText("");
}
and then after all the "if"s for the questions, I have code that verifies if it is correct or not, and if it's correct I increase the score. This is the code I use:
if(command.equals("Check")&& (txtAnswer.getText().equalsIgnoreCase("4") && (q1)))
{
q1= false;
lblOutcome.setText("Correct");
score = score +1;
lblScore.setText("Current Score: " + score);
}
else if(command.equals("Check")&& (!txtAnswer.getText().equalsIgnoreCase("4") && (q1)))
{
q1= false;
lblOutcome.setText("Incorrect");
lblScore.setText("Current Score: " + score);
}
if(command.equals("Check")&& (txtAnswer.getText().equalsIgnoreCase("8") && (q2)))
{
q2= false;
lblOutcome.setText("Correct");
score = score +1;
lblScore.setText("Current Score: " + score);
}
else if(command.equals("Check")&& (!txtAnswer.getText().equalsIgnoreCase("8") && (q2))
{
q2= false;
lblOutcome.setText("Incorrect");
lblScore.setText("Current Score: " + score);
}
But the problem I'm having right now is that the score is not increasing, it just completely resets to 0 after each question so if I get all the questions right, the score remains at 1. I was wondering what I have to do to fix this?
Edit: this is where wrote the code
private static class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String command = e.getActionCommand();
int score = 0;
Aucun commentaire:
Enregistrer un commentaire