While I transformed parts of my code into methods, an integer I mean to increase under a certain condition (the player score) doesn't. This is a series of methods inside other methods inside a single method that gets run in main, where everything but the integer increase works fine and it's really bugging me out.
I ended up with creating a public static int method for the point increase that gets called inside a public static void method that's basically an if function. If the player's answer was the correct answer, print out a "Correct!" message and call the player score increase method. This is where I left it at before caving in and creating a stack overflow account. Here's the full code
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
/*Questions*/
String[] questions = new String[5];
questions[0] = "What year did the hit cartoon show Tom & Jerry debut in for the first time?";
questions[1] = "In the famous japanese franchise Dragon Ball Z, most main characters can undergo what powerful transformation?";
questions[2] = "The famous video game character, Sonic, is of what animal species?";
questions[3] = "Java, the programming language, was created by whom?";
questions[4] = "The famous social platform, Roblox, launched for the first time in what year?";
/*Answers*/
String[] answers = new String[5];
answers[0] = "1941";
answers[1] = "super saiyan";
answers[2] = "hedgehog";
answers[3] = "james gosling";
answers[4] = "2006";
Scanner playerInput = new Scanner(System.in);
int playerScore = 0;
/*Trivia Start*/
System.out.println("Welcome to Jpeg Louie's Trivia v3 (now with methods!)");
System.out.println("You'll be expected to type the correct answer. Mind your whitespaces and punctuations");
System.out.println(" ");
while(true){
doTriviaQuiz(questions, answers, playerInput, playerScore);
/*Trivia End*/
System.out.println("----------------------------------------------------------");
System.out.println("The trivia is over! Thanks for playing :)");
System.out.println("CALCULATING SCORE");
System.out.println("...");
System.out.println("...");
System.out.println("...");
System.out.println("Your score was: " + playerScore + " correct questions out of " + questions.length);
/*Special Score Message*/
if(playerScore == 0){
System.out.println("Looks like you got all questions wrong...");
System.out.println("Try to get them all correct, I might reward you with something ;)");
} else if(playerScore == questions.length){
System.out.println("Congratulations! You beat the trivia quiz!");
System.out.println("Here's a list of commands that trigger special events");
System.out.println("(When prompted to play again, type the desired command to execute it)");
}
/*Play Again?*/
System.out.println(" ");
System.out.println("Play again? Type [Y] for Yes and anything else for No");
String playAgain = playerInput.nextLine();
if(playAgain.equalsIgnoreCase("y")){
System.out.println(" ");
} else {
break;
}
}
}
public static void doTriviaQuiz(String[] questions, String[] answers, Scanner playerInput, int playerScore) {
doQuestionsXTimes(questions, answers, playerInput, playerScore);
}
public static void doQuestionsXTimes(String[] questions, String[] answers, Scanner playerInput, int playerScore) {
for(int i = 0; i < questions.length; i++) {
doQuestion(questions, answers, playerInput, playerScore, i);
}
}
public static void doQuestion(String[] questions, String[] answers, Scanner playerInput, int playerScore, int i) {
System.out.println("Question " + (i + 1) + ": " + questions[i]);
String answer = playerInput.nextLine();
checkCorrectAnswer(answers, i, answer, playerScore);
}
public static void checkCorrectAnswer(String[] answers, int i, String answer, int playerScore) {
if (answer.equalsIgnoreCase(answers[i])) {
System.out.println("Correct!");
playerScore = incrementPlayerScore(playerScore);
} else {
System.out.println("Incorrect! The answer was: " + answers[i]);
}
}
public static int incrementPlayerScore(int playerScore){
playerScore++;
return playerScore;
}
}```