Rory A Campbell
Rory A Campbell

Reputation: 131

How do get this variable to be recognised later in the method?

Hi guys I'm writing a maths quiz program as a learning exercise and I cant get this 'response' variable to be recognised later in the method. Specifically the 'response' variable with *s either side of it does not link to the response variables declared earlier. I'm new to programming so fairly sure I'm making a basic error but I can't work it out and I'd be grateful if anyone can help me. Thanks!

import acm.util.*;
import acm.program.*;


public class MathsQuiz extends ConsoleProgram{

    public void run(){
        println("This program gives atudents a series of maths questions");
        askQuestions();
    }

    private void askQuestions(){
        for (int i = 0; i < NUMBER_OF_QS; i++){
            askQ();
        }
    }

    private void askQ(){
        int answer = rgen.nextInt(0,20);
        int number1 = rgen.nextInt(0,20);
        int number2 = answer - number1;

        if (number2 > 0){
            int response = readInt("What is " + number1 + "+" + number2 + "?");

        }else {
            int response = readInt("What is " + number1 + " " + number2 + "?");
        }

                if (**response** == answer){
                    println("Correct!");
                }else{
                    println("Incorrect");
                }
        }


    private RandomGenerator rgen = RandomGenerator.getInstance(); 

    int NUMBER_OF_QS = 5;
    int RES = 0;
}

Upvotes: 2

Views: 102

Answers (5)

user2542965
user2542965

Reputation:

response is a local variable, and its accessibility is limited to inside the block in which it has been declared. So, response is accessible only within the if else block. IF you try to use response outside the area of scope, you will get a compile time error, until you don't redeclare it.

So, if you want to use response out of the if else block, you have to declare it out of the block as follows:

int response;
if (number2 > 0){
     response = readInt("What is " + number1 + "+" + number2 + "?");
}else {
     response = readInt("What is " + number1 + " " + number2 + "?");
}

Upvotes: 1

Ahmed KRAIEM
Ahmed KRAIEM

Reputation: 10427

Move response to an outer scope:

int response;

if (number2 > 0) {
    response = readInt("What is " + number1 + "+" + number2 + "?");
} else {
    response = readInt("What is " + number1 + " " + number2 + "?");
}

Local variables have the most limited scope. Such a variable is accessible only from the function or block in which it is declared. The local variable's scope is from the line they are declared on until the closing curly brace of the method or code block within which they are declared.

Upvotes: 3

Ben
Ben

Reputation: 607

its because you are creating the variable response inside of the if statement. try this instead

private void askQ(){
int answer = rgen.nextInt(0,20);
int number1 = rgen.nextInt(0,20);
int number2 = answer - number1;
int response; //to create the variable in the right scope


if (number2 > 0){
    response = readInt("What is " + number1 + "+" + number2 + "?");

}else {
    response = readInt("What is " + number1 + " " + number2 + "?");
}

        if (**response** == answer){
            println("Correct!");
        }else{
            println("Incorrect");
        }
}

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500065

Just declare it before the if statement:

int response;
if (number2 > 0) {
    response = ...
} else {
    response = ...
}

Alternatively, spot the small amount of difference between the two blocks, and change just that using a conditional operator:

String separator = number2 > 0 ? "+" : " ";
int response = readInt("What is " + number1 + separator + number2 + "?");

Upvotes: 1

Sean Bright
Sean Bright

Reputation: 120644

You need to define response outside of the if statement:

int response = -1;

if (number2 > 0) {
    response = /* ... Something ... */
} else {
    response = /* ... Something else ... */
}

Upvotes: 1

Related Questions