user1057152
user1057152

Reputation:

How can I add up the sum of these scores for the final value?

This is my first block of code, my main class. I already have the question array and objects created, this is just to show you guys what I am trying to get done.

for(int i = 0; i<questionArray.length; i++)
{
  questionArray[i].askQuestion();
  questionArray[i].getAnswer();
  questionArray[i].checkAnswer();
}

These are my methods used in the loop above:

public void askQuestion()
{
   System.out.println(question);
   System.out.println("This question is worth " + pointValue + " point(s)");
   System.out.println("Please enter your answer: ");
}

public String getAnswer()
{
    return answer;
}

public boolean checkAnswer()
{
  Scanner keyboard = new Scanner(System.in);
  String UserAnswer = keyboard.nextLine();
  if (UserAnswer.equalsIgnoreCase(answer))
  {
    System.out.println("congratulations you have earned " + pointValue + " point(s)\n");
    return true;
  }
  else 
  {
    System.out.println("sorry you did not get the answer correct");
    System.out.println("The correct answer is " + this.answer + "\n");
    return false;
  }
}

So as you can see this is a TriviaGame class, 1.)I am trying to make two methods, one that is capable of displaying the pointValue(only when user answer == answer) after each question. 2.)The second one is adding up each of the pointValue(again when answers are equal. A.k.a checkAnswer == true) when the user gets the answer correct, and display all the results at the end.

Upvotes: 0

Views: 1938

Answers (4)

Renuz
Renuz

Reputation: 1657

The easiest and simplest way to do this is to just use a instance or static variable to keep track of it.

Upvotes: 1

Tyler
Tyler

Reputation: 301

In your first block of code, where you run each method within the for loop, you should run questionArray[i].updateScore() instead of questionArray[i].checkAnswer() . As of right now, you are running checkAnswer() which gets an answer from the user and checks to see whether it is correct. But if you plan to call updateScore() after this, I believe you will end up running checkAnswer() again within the condition of the if statement, thus collecting an answer from the user again without meaning to. By running just updateScore() , you will both gather the answer AND update the score. I'm not sure if you knew this already, but I thought I'd let you know just in case.

As for your initial question, you could try this: Create an boolean instance variable for your Question class (initialized to false) that will be set to true if their answer was correct. This way, you can iterate through your questionArray and just check to see whether their answer was correct or not. If it was answered correctly, then add the Question's pointValue to your total score.

I hope I understood your question and I hope this helps,
Tyler

Upvotes: 1

Thomas
Thomas

Reputation: 1160

Do you want to use the variable score to aggregate the points earned so far? Then you have to make it an attribute of the class or declare it as static, should initialize it with 0 and, in fact, have to add the score earned each time, as Bohemian already told you.

int score = pointValue;

as well as

int score += pointValue; 

is local to your updateScore method and it's value will be lost when the method is left. If you declare

static int score = 0;

Rgds,

Thomas

Upvotes: 0

Bohemian
Bohemian

Reputation: 425033

did you mean:

 int score += pointValue; // note the plus (+)



As an aside, consider changing if (checkAnswer() == true) to if (checkAnswer())

Upvotes: 2

Related Questions