Quiz Game Using Java

Quiz Game Using Java

Introduction:

In this article, we will show how to develop a Quiz Game. With the use of this quiz game Command Line User Interface(CLI) system, you may create a quiz, take a quiz, view quiz questions and its answers and list the quizzes easily and quickly. The system gives an easy-to-use interface having basic commands for creating, taking, viewing and listing all available quizzes. So grab a seat, pour a cup of coffee and start playing quiz game.

Explanation:

This is a Quiz Game with a Command Line User Interface that allows you to easily create, take, view and list the quiz. Users may easily create quizzes, play quizzes, view the questions and answers and list the quizzes that are available. The Quiz Game has a simple design, with commands like create, take, view, list and exit.

The user can use create command to create a quiz, take command to take the quiz, view command to view the quiz, list command to display all of the quizzes that are currently available and exit command to end the quiz game. The command will be quickly followed by the system which will act accordingly.

The quiz game is Command Line User Interface system. The system uses the while loop, for loop, HashMap class and Scanner class. Specified methods, such as createQuiz(), takeQuiz(), viewQuiz() and listQuiz() are used to handle the commands. Using the HashMap class and array list the quiz questions and answers are inserted into the game.

Methods used in the project:

createQuiz():

If the user gives the command to create a quiz, this method is triggered. The method initially asks for the name of the quiz, the number of questions to be added, and choices of each question, and the correct answer to the question. Once the quiz is created message is displayed.

takeQuiz():

If the user wishes to play the quiz game, the take command is used. takeQuiz() method is invoked when the take command is given to the system. Initially, the system will ask on which topic the user wants to play the quiz game. The questions are displayed with options for the given topic. At last, the achieved score is displayed to the user.

viewQuiz():

When the user gives view command to the system, the viewQuiz() method is triggered. Initially the method asks the name of the quiz, and if it is found it displays the quiz questions and its answer to the users otherwise the not found message is displayed.

listQuiz():

When the user gives list command to the system, the listQuiz() method is invoked. This method displays the list of all quiz topics that are available in the game.

Source Code:

				
					Source Code:
import java.util.*;

public class QuizGame {

    private static Map<String, Quiz> quizzes = new HashMap<>();

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("Enter a command: (create, take, view, list, exit)");
            String command = scanner.nextLine();
            if (command.equals("create")) {
                createQuiz(scanner);
            } else if (command.equals("take")) {
                takeQuiz(scanner);
            } else if (command.equals("view")) {
                viewQuiz(scanner);
            } else if (command.equals("list")) {
                listQuizzes();
            } else if (command.equals("exit")) {
                break;
            } else {
                System.out.println("Invalid command.");
            }
        }
    }

    private static void createQuiz(Scanner scanner) {
        System.out.println("Enter the name of the quiz:");
        String quizName = scanner.nextLine();
        Quiz quiz = new Quiz(quizName);
        System.out.println("Enter the number of questions:");
        int numQuestions = Integer.parseInt(scanner.nextLine());
        for (int i = 0; i < numQuestions; i++) {
            System.out.println("Enter the question:");
            String question = scanner.nextLine();
            System.out.println("Enter the number of choices:");
            int numChoices = Integer.parseInt(scanner.nextLine());
            List<String> choices = new ArrayList<>();
            for (int j = 0; j < numChoices; j++) {
                System.out.println("Enter choice " + (j+1) + ":");
                String choice = scanner.nextLine();
                choices.add(choice);
            }
            System.out.println("Enter the index of the correct choice:");
            int correctChoice = Integer.parseInt(scanner.nextLine()) - 1;
            quiz.addQuestion(new Question(question, choices, correctChoice));
        }
        quizzes.put(quizName, quiz);
        System.out.println("Quiz created.");
    }

    private static void takeQuiz(Scanner scanner) {
        System.out.println("Enter the name of the quiz:");
        String quizName = scanner.nextLine();
        Quiz quiz = quizzes.get(quizName);
        if (quiz == null) {
            System.out.println("Quiz not found.");
            return;
        }
        int score = 0;
        for (int i = 0; i < quiz.getNumQuestions(); i++) {
            Question question = quiz.getQuestion(i);
            System.out.println("Question " + (i+1) + ": " + question.getQuestion());
            List<String> choices = question.getChoices();
            for (int j = 0; j < choices.size(); j++) {
                System.out.println((j+1) + ": " + choices.get(j));
            }
            System.out.println("Enter your answer:");
            int userAnswer = Integer.parseInt(scanner.nextLine()) - 1;
            if (userAnswer == question.getCorrectChoice()) {
                System.out.println("Correct!");
                score++;
            } else {
                System.out.println("Incorrect. The correct answer is " + (question.getCorrectChoice()+1) + ".");
            }
        }
        System.out.println("Your score is " + score + " out of " + quiz.getNumQuestions() + ".");
    }

    private static void viewQuiz(Scanner scanner) {
        System.out.println("Enter the name of the quiz:");
        String quizName = scanner.nextLine();
        Quiz quiz = quizzes.get(quizName);
        if (quiz == null) {
            System.out.println("Quiz not found.");
            return;}
                System.out.println("Quiz: " + quiz.getName());
    for (int i = 0; i < quiz.getNumQuestions(); i++) {
        Question question = quiz.getQuestion(i);
        System.out.println("Question " + (i+1) + ": " + question.getQuestion());
        List<String> choices = question.getChoices();
        for (int j = 0; j < choices.size(); j++) {
            System.out.println((j+1) + ": " + choices.get(j));
        }
        System.out.println("Answer: " + (question.getCorrectChoice()+1));
    }
}
    
private static void listQuizzes() {
    System.out.println("Quizzes:");
    for (String quizName : quizzes.keySet()) {
        System.out.println("- " + quizName);
    }
}
}

class Quiz {
    private String name;
private List<Question> questions = new ArrayList<>();

public Quiz(String name) {
    this.name = name;
}

public String getName() {
    return name;
}

public void addQuestion(Question question) {
    questions.add(question);
}

public Question getQuestion(int index) {
    return questions.get(index);
}

public int getNumQuestions() {
    return questions.size();
}
}
class Question {
    private String question;
private List<String> choices;
private int correctChoice;

public Question(String question, List<String> choices, int correctChoice) {
    this.question = question;
    this.choices = choices;
    this.correctChoice = correctChoice;
}

public String getQuestion() {
    return question;
}

public List<String> getChoices() {
    return choices;
}

public int getCorrectChoice() {
    return correctChoice;
}

}
 
				
			

Output:

Find More Projects

Hostel Management System Using Python With Source Code by using Python Graphical User Interface GUI Introduction: Managing a hostel efficiently can be …

Contra Game Using Python with source code using Pygame Introduction: Remember the super fun Contra game where you fight aliens and jump …

Restaurant Billing Management System Using Python with Tkinter (Graphical User Interface) Introduction: In the bustling world of restaurants, efficiency is paramount, especially …

Jungle Dash Game Using Python with source code with pygame module Introduction: Dive into the excitement of jungle adventures with our new …

Building a Tetris Game with Python with source code Introduction: Welcome to the world of Tetris, where falling blocks meet strategic maneuvers …

Super Mario Game Using Python with source code Introduction: Remember playing Super Mario as a kid? It’s that classic game where you …

All Coding Handwritten Notes

Browse Handwritten Notes