Question

I have made a code witch asks you questions and for every question you get right it adds one point, the problem is i want to know how to show your score. Like how many points you have all together.

System.out.println("#1: What is 8+8?");
answer=in.nextInt();
if (answer == 16)           
    System.out.println("Correct!");
else
    System.out.println("Incorrect!");
if (answer == 16)
    points =+ 1;

That is an example showing that if the "answer" is correct it will +1 point. I would like for when it tells you it is correct how many points you have. Example: "Correct! You now have "points" point(s)! Here are my ints if you need them

int points;
int answer2;
int answer;

I only have 2 questions so far because i am waiting for this solution.

Was it helpful?

Solution 2

Keep a counter variable that will hold the number of correct answers.

Integer scoreCount = 0; //initial score

if (answer == 16) {
    scoreCount += 1; //increase score count           
    System.out.println("Correct! You now have " + scoreCount + " points"); //show latest score
}    

OTHER TIPS

System.out.println("Correct! You now have "+points+" points!");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top