-1

I know that there are many questions like this one but it didn't really help me out any. I have to Write a program that will help an elementary school student learn multiplication. Use a SecureRandom object to produce two positive one-digit integers. The program should then prompt the user with a question, such as "How much is 6 times 7?" I have done the hard part but I can not get my variables that is in my class to work for the other methods. i will "get non-static variable randomNumbers cannot be referenced from a static context" error! can someone help me please!!

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

public class computerAssistedInstruction {
    Random randomNumbers = new Random();
    int answer;// the right answer

    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);

        int input;// the user answer
        newQuestion();
        input = scnr.nextInt();
        if (input != answer) {
            System.out.println("No. Please try again.");
        }// end of if
        else {
            System.out.print("Very Good!");
            newQuestion();
        }// end of else
    }// end of main

    public static void newQuestion() {
        int number1 = randomNumbers.nextInt(10);
        int number2 = randomNumbers.nextInt(10);
        answer = number1 * number2;
        System.out.printf("How much is %d times %d?", number1, number2);
    }// end of newQuestion
}// end of class
Saif Ahmad
  • 1,118
  • 1
  • 8
  • 24
  • Use `static Random randomNumbers = new Random();`, or create an instance of `computerAssistedInstruction` and use that. – Tim Biegeleisen Apr 16 '18 at 07:11
  • ... and then use `static int answer;` or you'll get the same error with `answer` instead of `randomNumbers` – Rafalon Apr 16 '18 at 07:12

1 Answers1

1

Problem

The error is

non-static variable randomNumbers cannot be referenced from a static context

which means that since you declared randomNumbers like this

public class computerAssistedInstruction {
     Random randomNumbers = new Random();
     int answer;
}

you cannot use it (or answer) in your static method

public static void newQuestion() { ... }

Solution

Either make randomNumbers and answer static variables, or make newQuestion an instance method (i.e. not static) like so:

public class computerAssistedInstruction {
     Random randomNumbers = new Random();
     int answer;

     ...

     public void newQuestion() {
          ...
     }
}

Now, you need to edit your main method a little since you cannot call the non-static method without an instance of the class anymore. So, instead of

public static void main(String[] args) {
    Scanner scnr = new Scanner(System.in);
    int input;

    newQuestion();
    input = scnr.nextInt();

    if (input != answer) {
        System.out.println("No. Please try again.");
    }
    else {
        System.out.print("Very Good!");
        newQuestion();
    }
}

you would now need this:

public static void main(String[] args) {
    computerAssistedInstruction instance = new computerAssistedInstruction(); // Create an instance of the class here.
    Scanner scnr = new Scanner(System.in);
    int input;

    instance.newQuestion(); // Call newQuestion() on the instance.
    ^^^^^^^^^^^^^^^^^^^^^^^

    input = scnr.nextInt();

    if (input != answer) {
        System.out.println("No. Please try again.");
    }
    else {
        System.out.print("Very Good!");
        instance.newQuestion();
        ^^^^^^^^^^^^^^^^^^^^^^^
    }
}
Thomas Flinkow
  • 4,845
  • 5
  • 29
  • 65