Java Quiz Game

by connors16 in Circuits > Software

4086 Views, 1 Favorites, 0 Comments

Java Quiz Game

Screenshot 2023-03-15 235530.png

Using the Java programming language, people can utilize it to create a variety of practical or fun applications, including simple calculators, tic tac toe, or even hangman. Another exciting Java project is a simple trivia game which prompts the user with questions and determines their score based off their answers.


Today, I'll walk explain the steps needed to make your very own quiz game using the Java programming language. Regardless of whether or not you have prior programming experience, I'm sure this short project will help you gain a better understanding of Java and further hone your skills.

Supplies

To create this quiz game, you'll need

  • A JDK(Java Development Kit)
  • A Java IDE(Integrated Development Environment)

Both these software applications can be downloaded online. Also, considering the different programming experience people have using Java, this project will take roughly 30-60 minutes.

Download JDK(Java Development Kit)

Screenshot (38).png

Begin by first downloading a JDK using the Oracle website.

On the website, they have downloading instructions for Windows, MacOS, and Linux devices, so the computer you download the JDK onto isn't of importance since its compatible with them all. Oracle also provides you with the choice between downloading either the Java 19 or Java 17 versions. Truthfully, the version you download won't affect how you proceed but is rather a personal preference.


If you wish to visit the Oracle website now, click this link.


WARNING

Take great caution and be sure to safely download a JDK from Oracle and not any suspicious sites potentially harboring viruses.

Download Java IDE(Integrated Development Environment)

Screenshot 2023-02-24 010732.png

Continue by now downloading an IDE onto your computer, preferably Eclipse since I completed the quiz game using its features.

Eclipse's installer is compatible with Linux, MacOS, and Windows, and it may take a brief period to fully download the software. Also, you will want to ensure you're downloading the "Eclipse IDE for Java Developers" and not other developers such as C++.


If you're trying to make sure you download the right version of Eclipse, click this link


Note:

Although I used Eclipse making the quiz game, many other great IDEs exist to use online, however, you may have a difficult time following instructions if you make that decision.

Open IDE Software and Create New Java Project

Screenshot 2023-02-24 015115.png

Start opening Eclipse once it has finished downloading onto your device.

When first entering the Eclipse workspace, you'll want to click the File button in the top left, find New, and then Java Project. After, you'll simply name your project and save to your workspace


Note:

When creating your project, you only want to use the default package inside it. You won't use the JRE System Library which is also present.

Create New Java Class

Screenshot 2023-03-15 201721.png

Proceed by now creating a new java class for the newly created project.

Go into the src folder within the project and find the default package. You'll right-click the package, find New, and select the Class option. You can name the class anything you want but for mine, I named my class QuizGame. Then, click finish.


Note:

For those who may not have much programming experience, when naming items such as classes or variables, one typically follows camelcase formatting. This means the phrase's individual words of the class/variable have the first letter of each word capitalized using no spaces. An example would be the title I chose when writing my class, which is QuizGame.

Import Scanners

Screenshot 2023-03-15 205212.png

Go into the newly created class inside your java project.

Using the line above the class declaration where you should see "public class (classname)", you're going to include the text shown in the attached image. This will import the scanner class into your program, which will have use later.


Note:

Providing a bit more information about scanners in java, they mainly receive user input, which can then be stored using data types such as int, double, String, etc.

Create 2D Array in Main Body

Screenshot 2023-03-15 211435.png

Declare and instantiate a new 2D array in the main method of our class.

Inside the 2D array, you'll store the question answer pairs you'll include inside your game. However, you don't have to use the exact same questions and answer's from the example and can use whatever you want if you keep the formatting the same.

Here's a link to read up more on 2D arrays.


Note:

The syntax when programming with java requires specificity ito function properly, so take extra care to ensure you're created 2D array follows the same format as the attached example.

Create a Variable for the Player's Score

Screenshot 2023-03-15 213924.png

Declare and instantiate a new int variable following the 2D array created previously.

This new int variable represents an integer which will keep track of the player's score throughout the game. In the attached example, the variable is named score, but you can use whatever name you want if you reference the same name throughout the program.


Note:

Simply put, java variables are containers which store data values of various types, including int, double, String, char, etc. Strings store textual values similar to "Hi" or "Hello".

Create a Loop and 2 String Variables to Iterate Q&As

Screenshot 2023-03-15 220552.png

Create a new "for loop", which will allow you to traverse the 2D array and present the user with the questions you decided using the attached example.

Inside the loop, you'll declare and instantiate two string variables which represent the respective question and answer to each value in the 2D array.

Here's a link which details creating for loops and also their purpose in programming

Here's another link which explains the iteration of arrays using a for loop.

Set Up Scanner to Store Answers to Questions

Screenshot 2023-03-15 223136.png

Write a print statement first which will simply reference the previously mentioned string variable for questions and prompt the user's answer.

Declare and instantiate a scanner which will allow you to store the user's answer to the proposed question. Then, use the scanner to store the user's answer in its own string variable.

Here's a link to creating scanners in java and their methods so you can better understand their intended purpose and learn how to store a string value from a scanner.

Create If-else Block and Print Statements Inside

Screenshot 2023-03-15 224437.png

Continue after the scanner setup using an if-else block following the attached example to determine the correctness of the user's entered answer, which was recorded by the scanner.

For each possible outcome, you should also include a print statement which indicates to the user whether or not they got the prompted question right or wrong.

Here's a link to using if-else statements so you can determine how to proceed depending on the user's entered answer


Note:

You can write different print statements than the ones used in the example if it clearly displays whether the user was right or wrong.

Increase User's Score If Correct and Include Last Print Statement

Screenshot 2023-03-15 230312.png

Increment the user's score value if their inputted answer matched the correct answer from the question they were given.

Don't forget to write another last print statement which lies both outside the if-else statement and for loop, which will display the user's final score out of the total number of questions asked. However, you can calculate score another way if you desire.