1

I have created a math game that asks ten random questions and grades you at the end. I created an integer variable named score and initialized it as 0. In the if statement, you are awarded 10 points if you answer correctly. I figured since I have score + 10 inside the for loop then I shouldn't have to use increments for the scoring system. If anyone could point me in the right direction on why Java is telling me that the score variable is not being used, I would greatly appreciate it. Cheers from Texas!

package pkgnew;

import java.util.Scanner;
import java.util.Random;

public class New {

    public static void main(String args[]) {

        //Game 10x loop        
        for(int i = 0; i < 10; i++)
        {

        //Declare and construct variables 
        Random randomnum = new Random();
        int fnum, snum;
        int mathnumber = randomnum.nextInt(20);
        int newnumber = randomnum.nextInt(20);

        //Declare and construct variables of math problem
        fnum = mathnumber;
        snum = newnumber;

        //Declare random operator variable
        String[] operators = {"+" , "-", "*", "/" };
        int randomIndex = randomnum.nextInt(3);
        String symbol = operators[randomIndex];

        //Initialize answer and score
        int answer = 0;
        int score = 0;

        //Switch statement for random operator and question display
        switch (symbol) {
            case "+": 
                System.out.println(fnum + "+" + snum);
                answer = fnum+snum;
                break;
            case "-":
                System.out.println(fnum + "-" + snum);
                answer = fnum-snum;
                break;
            case "*":
                System.out.println(fnum + "*" + snum);
                answer = fnum*snum;
                break;
            case "/":
                System.out.println(fnum + "/" + snum);
                answer= fnum/snum;
                break;
        }

        //User input
        Scanner serena = new Scanner(System.in);
        int userAnswer = serena.nextInt();

        //If user input = answer display "correct"
        if (userAnswer == answer) {
        System.out.println("Correct!");
        score = + 10;

        //If user input does not = answer display "wrong" and correct answer
        } else {
        System.out.print("Wrong! The correct answer is: " );
        System.out.println(answer);
        }

        }

        System.out.println("Game Over!");
        System.out.println("Your score is:");
        System.out.println(score);


    }
}

I use Java 8 and NetBeans 8.0.

ligi
  • 39,001
  • 44
  • 144
  • 244
  • 2
    score = + 10; does this even compile? – JamesB May 24 '14 at 16:39
  • 4
    It compiles but it does not do what he needs. That just makes the number positive and assigns it to the variable, but that is basically pointless because the number is always going to be positive. Fix it by simply saying `score += 10`. You should know that this is the same as `score = score + 10`, it is just a syntactic sugar improving readability. – Lynx May 24 '14 at 16:41
  • Thank you Lynx! I fixed the syntax. On the final line that displays your score Java says that it cannot find the variable score. On line 31, when score is initialized, it says that it is never used. I have the feeling that I may not be using the variable correctly. – Kunimaro15689 May 24 '14 at 16:51
  • edited my answer. It is a simple scope problem – Lynx May 24 '14 at 16:55
  • The thing is that at the end, it does not print the final score because it says the variable is not used. If this is a problem with my IDE, how do I fix it? – Kunimaro15689 May 24 '14 at 16:57
  • Did you remove the int score inside the for loop? – Lynx May 24 '14 at 16:58
  • Do you want me to give your code with the mistakes fixed? – Lynx May 24 '14 at 17:01

2 Answers2

2

You get the problem because you are initializing the variable inside the for loop, and then trying to use it outside which is not possible simply because that is how the language is made. This is called scope. And you should know that a variable is alive only in the scope (in the brackets) that has been declared. Like in your case it is available only in the for loop and then it is being collected by the garbage collector. It is nothing serious, just define the score variable just before implementing the for loop.

int score;

for(int i...)
{
    // ...
}

// now you can use score here:

There is one more thing: you are not incrementing your score variable at all. You are just saying score = +10. This is just making the positive number 10, positive, assigns it to the variable and whatever data the variable score had, it no longer has it which is not what you want to do. The correct way, which I think you wanted to use is by using the += operator like this: score += 10;. += is a syntactic sugar which is used to improve readability and does absolutely the same as saying score = score + 10;.

Lynx
  • 506
  • 2
  • 10
  • You will still need to initialize `score=0;` before loop. (And the variable will be allocated on stack, not on heap, so garbage collercor will have nothing to do with it's destruction) – kajacx May 24 '14 at 17:01
  • 1
    If you do not specify the initial value of an integer it will be zero, because that is a value data type. Every value data type has its default value (e.g bool - false, double - 0 ect). And the second thing is that an value type like the integer cannot be allocated on the heap. Its always kept in the stack no matter what. You understood things a little wrong. Google value vs reference data types for more information. – Lynx May 24 '14 at 17:04
  • Only field variables are assigned default values in Java, local variables are not, see http://stackoverflow.com/questions/9687634/java-variable-default-value . You agree that `score` will be allocated on stack, thusly garbage collectr will not dealocate it, see http://stackoverflow.com/questions/2447504/is-the-stack-garbage-collected-in-java – kajacx May 24 '14 at 17:16
1

Your problem is scope. If you define a variable inside a loop, then nothing outside the loop will be able to see it.

package pkgnew;

import java.util.Scanner;
import java.util.Random;

public class New {

    public static void main(String args[]) {


        //Score must go here, outside loop
        int score = 0;

        //Game 10x loop        
        for(int i = 0; i < 10; i++)
        {
            //Logic

        }
        System.out.println("Game Over!");
        System.out.println("Your score is:");
        //Otherwise score is not defined here
        System.out.println(score);


    }
}
slevin
  • 308
  • 1
  • 8