vendredi 11 novembre 2022

How can I make my Random Math Game loop in Java

So I've been searching on the internet on how to make my Random Math Game loop instead of exit the program when reaching the end of the game, and make the program go back to the start where we can choose the menu available, but I can't seem to find the answer. I've been watching some tutorials on YouTube explaining that I can achieve what I want by using the while loop, but I still can't find a right way to put the while loop in my code.

Here is my code:

package main;

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

public class Main {
    
    static void gameStartStatement() {
        System.out.println();
        System.out.println("+++++++ Game start! +++++++");
        System.out.println();
    }
    static int rngGenerated() {
        Random rng = new Random();
        int generatedNumber = rng.nextInt(20) + 1;
        return generatedNumber;
    }
    
    public static void main(String[] args) {
        Scanner inputMenu = new Scanner(System.in);
        Scanner answerInput = new Scanner(System.in);

        int userInput;
        int userAnswer;
        int maxQuestion = 20;
        int question = 1;
        int scoreResult = 0;
        int x;
        int y;
        int z;

        System.out.println("++ Select your Math Game ++");
        System.out.println(":=========================:");
        System.out.println("| 1> Addition             |");
        System.out.println("| 2> Subtraction          |");
        System.out.println("| 3> Multiplication       |");
        System.out.println(":=========================:");
        System.out.print(">> ");
        userInput = inputMenu.nextInt();

        if (userInput == 1) {
            
            gameStartStatement();
            for (question = 1; question <= maxQuestion;) {

                x = rngGenerated();
                y = rngGenerated();
                z = x + y;

                System.out.println(question + ".");
                System.out.println("What is " + x + " + " + y + " ?");
                System.out.print("Answer >> ");
                userAnswer = answerInput.nextInt();

                if (userAnswer == z) {
                    System.out.println("Correct");
                    System.out.println();
                    scoreResult++;
                } else {
                    System.out.println("Incorrect");
                    System.out.println();
                }
                question++;
                
            }

        } else if (userInput == 2) {
            
            gameStartStatement();
            for (question = 1; question <= maxQuestion;) {

                x = rngGenerated();
                y = rngGenerated();
                z = x - y;

                System.out.println(question + ".");
                System.out.println("What is " + x + " - " + y + " ?");
                System.out.print("Answer >> ");
                userAnswer = answerInput.nextInt();

                if (userAnswer == z) {
                    System.out.println("Correct");
                    System.out.println();
                    scoreResult++;
                } else {
                    System.out.println("Incorrect");
                    System.out.println();
                }
                question++;
            }


        } else if (userInput == 3) {
            
            gameStartStatement();
            for (question = 1; question <= maxQuestion;) {

                x = rngGenerated();
                y = rngGenerated();
                z = x * y;

                System.out.println(question + ".");
                System.out.println("What is " + x + " x " + y + " ?");
                System.out.print("Answer >> ");
                userAnswer = answerInput.nextInt();

                if (userAnswer == z) {
                    System.out.println("Correct");
                    System.out.println();
                    scoreResult++;
                } else {
                    System.out.println("Incorrect");
                    System.out.println();
                }
                question++;
            }

        }
        System.out.println("You answered " + scoreResult + "/20 questions correct");
        System.out.println("You scored " + (scoreResult * 5));
    }
}

I can't really find the solution to my problem.




Aucun commentaire:

Enregistrer un commentaire