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?