0

I'm trying to make a Math Quiz but I started learning Java recently and I'm having some trouble. I already read about accessing methods and stuff, but I don't see how all of this is supposed to help me there.

    import java.util.Scanner;

public class MatQui{
        Scanner scan = new Scanner(System.in);
    
    int correct = 0;
    
public static void main(String[] args) {
    
    System.out.println("Enter your name: ");
    String name = scan.nextLine();
    
    System.out.println("Hello " + name + "! Answer the questions.");
    
    firstQuiz();
    
}

public void firstQuiz() {
    
    int randomNum1 = (int)(Math.random() * 101 +1);
    int randomNum2 = (int)(Math.random() * 101 +1);
    
    int RandomAddSolution = scan.nextInt();
    int CorrectAddSolution = randomNum1 + randomNum2;
    
    System.out.println(randomNum1 + " + " + randomNum2 + " = ?");
    
    if (RandomAddSolution == CorrectAddSolution) {
        System.out.println("Correct!");
        correct++;
    }
    else if (RandomAddSolution != CorrectAddSolution) {
        System.out.println("Wrong! The correct answer is: " + CorrectAddSolution);
    }
    secondQuiz();
}

public void secondQuiz() {
    
    int randomNum1 = (int)(Math.random() * 101 +1);
    int randomNum2 = (int)(Math.random() * 101 +1);
    
    int RandomMinusSolution = scan.nextInt();
    int CorrectMinusSolution = randomNum1 - randomNum2;
    
    System.out.println(randomNum1 + " - " + randomNum2 + " = ?");
    
    if (RandomMinusSolution == CorrectMinusSolution) {
        System.out.println("Correct!");
        correct++;  //Ly Huong Van
    }
    else if (RandomMinusSolution != CorrectMinusSolution) {
        System.out.println("Wrong! The correct answer is: " + CorrectMinusSolution);
    }
    thirdQuiz();
}

public void thirdQuiz() {
    
    int randomNum1 = (int)(Math.random() * 11 +1);
    int randomNum2 = (int)(Math.random() * 11 +1);
    
    int RandomMulSolution = scan.nextInt();
    int CorrectMulSolution = randomNum1 * randomNum2;
    
    System.out.println(randomNum1 + " + " + randomNum2 + " = ?");
    
    if (RandomMulSolution == CorrectMulSolution) {
        System.out.println("Correct!");
        correct++;
    }
    else if (RandomMulSolution != CorrectMulSolution) {
        System.out.println("Wrong! The correct answer is: " + CorrectMulSolution);
    }
    fourthQuiz();
}

public void fourthQuiz() {
    
    int randomNum1 = (int)(Math.random() * 101 +1);
    int randomNum2 = (int)(Math.random() * 11 +1);
    
    int RandomDivSolution = scan.nextInt();
    int CorrectDivSolution = randomNum1 / randomNum2;
    
    System.out.println(randomNum1 + " / " + randomNum2 + " = ?");
    
    if (RandomDivSolution == CorrectDivSolution) {
        System.out.println("Correct!");
        correct++;
        endingScreen();
    }
    else if (RandomDivSolution != CorrectDivSolution) {
        System.out.println("Wrong! The correct answer is: " + CorrectDivSolution);
        endingScreen();
    }
}

public void endingScreen() {
    int percentageCorrect = correct * 25;
    System.out.println("You answered " + correct + " questions correctly.!\n"
    + "That's "  + percentageCorrect + "%!");
    
    if (correct == 0) {
        System.out.println(":(");
    }
    else if (correct != 0) {
        System.out.println(":)");
    }
}

I want to access 'firstQuiz()' method from main, but I get the error, that I can't access non static element form static main. How do I work with it?

  • You need an instance of this class in your `main`... What's the name of the class? – deHaar Sep 16 '20 at 07:44
  • You need an instance of the class. In `main` you don’t have one yet since it is `static`. Use `new` to create an instance inside of `main`. – Always Learning Sep 16 '20 at 07:44
  • You have four separate methods containing almost the same code. You should really rewrite this so it'll become a single method, where you use parameters to handle the variants of the method. – MC Emperor Sep 16 '20 at 07:50
  • Sorry, I edited the code. I must've missed that I didn't copy the beginning. –  Sep 16 '20 at 07:50
  • 1
    You should also follow the Java Naming Conventions: variable names and method names should be written in camelCase. – MC Emperor Sep 16 '20 at 07:51

5 Answers5

2

Non Static methods can be accessed by creating objects of the class in which the method is present.

In this example, you will have to create the object of the class in the main method and call the method firstQuiz() using this object.

Vishal Gada
  • 201
  • 3
  • 12
2

As tieburach answered... you can not access non static method from static one. You can not change main method signature, which means you can non make main non static. Main will always be static.

Solution would be to make your method static, but if it is not what you want, you can also create another class and instance of it in main method, and then call method on that class.

Wortig
  • 963
  • 2
  • 11
  • 37
1

As you probably noticed, the error says that you can't access non static method from static one. The easiest solution for you is to make the methods you are invoking static as well.

tieburach
  • 61
  • 4
  • But that would put me in a loop of every method being static and it's not really something I want to do. –  Sep 16 '20 at 07:45
1

You can make the method static or you create a new instance of the class and access to it like that:

Test test = new Test();
test.yourMethod();
1

You need create a new MatQui matqui = new MatQui(); and after call the method with new object created matqui.firstQuiz();