0

I'm currently a senior high school student and I'm doing a short quiz game as a project. And I was wondering if you can add multiple questions. Here Is the code

import java.util.Scanner;
import java.util.Random;
public class Quiz 
{

    public static void main(String[] args) 
    {
        Scanner s = new Scanner(System.in);
        Random r = new Random();
        int lives = 3;
        String answers;     
        
        while (lives > 0)
        {
            
            System.out.println("Question Goes Here: ");
            answers = s.nextLine();
            
                if (answers.equalsIgnoreCase("Answer")) 
                {
                    System.out.println("Correct! Please press ENTER to continue");
                    s.nextLine();
                }
                
                
            
                
                //TO-DO IF THE ANSWER IS WRONG
                else
                    {
                        --lives;
                        System.out.println("You have " + lives + " lives left");
                    }
            
        }
        
        
            //TO-DO IF "LIVES" IS EQUAL TO 0
            if (lives == 0) 
            {
                System.out.println("Game Over");
            }
        
    }
tgdavies
  • 10,307
  • 4
  • 35
  • 40
  • You can add as many questions as you want in an array and then take them from there – ABC Oct 31 '20 at 08:21

3 Answers3

1

Assuming that you have prepared a 2D array of questions and answers:

final Scanner s = new Scanner(System.in);

String[][] arrQA = {
    {"question1", "answer1"},
    {"question2", "answer2"},
    {"question3", "answer3"},
    {"question4", "answer4"},
};
int lives = 3;
int success = 0;

for (int i = 0; i < arrQA.length && lives > 0; i++) {
    String[] qa = arrQA[i];

    System.out.print(qa[0] + " goes here, type your answer: ");
    String answer = s.nextLine();
        
    if (answer.equalsIgnoreCase(qa[1])) {
        success++;
        System.out.println("Correct! Please press ENTER to continue");
        s.nextLine();
    } else {
        --lives;
        System.out.println("Incorrect! You have " + lives + " lives left");
    }
}
System.out.println("Game Over! You have answered " + success + " questions correctly");
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
  • Another question what does "[]" mean here after the String[] `String[] [] arrQA = { {"First Question: ", "answer1"}, {"Second Question: ", "answer2"}, {"Third Question: ", "answer3"}, {"Fourth Question: ", "answer4"}, };` – Aaron Roxas Oct 31 '20 at 14:06
  • `String[][]` means ["array of String arrays"](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html), it is a way of creating multidimensional arrays in Java. – Nowhere Man Oct 31 '20 at 14:25
1

I would recommend creating a POJO class for questions. Something like this:

    class Question{
        String question;
        String answer;
    
        public Question(String question, String answer) {
            this.question = question;
            this.answer = answer;
        }
    
        public String getQuestion() {
            return question;
        }
    
        public String getAnswer() {
            return answer;
        }
    }

And then create your main function, with the list of questions, iterate over your question with some variable.

public static void main(final String[] args) {
    //Variables
        final Scanner s = new Scanner(System.in);
        final Random r = new Random();
        int lives = 3;
        String answers;
        ArrayList<Question> quizBrain = new ArrayList<Question>();
    
        quizBrain.add(new Question("question1", "answer1"));
        quizBrain.add(new Question("question2", "answer2"));
        quizBrain.add(new Question("question3", "answer3"));
        quizBrain.add(new Question("question4", "answer4"));
        quizBrain.add(new Question("question5", "answer5"));

        int questionCounter = 0;
        //Conditions
        while (lives > 0)
        {
            System.out.println(quizBrain.get(questionCounter).getQuestion());
            answers = s.nextLine();
                if (answers.equalsIgnoreCase(quizBrain.get(questionCounter).getAnswer())) 
                {
                    System.out.println("Correct! Please press ENTER to continue");
                    questionCounter++;
                    s.nextLine();
                }
                //TO-DO IF THE ANSWER IS WRONG
                else
                    {
                        --lives;
                        System.out.println("You have " + lives + " lives left");
                    }   
        }
            //TO-DO IF "LIVES" IS EQUAL TO 0
            if (lives == 0) 
                System.out.println("Game Over");

        }

You can see this for the advantages of POJO class. It would be a great help if you need to store your question in the database.

sn-
  • 466
  • 1
  • 5
  • 15
0

I recomend you to create an array with all the questions and then acces it with math.random. You will also have to create an array for the answers in the same order as the question's one, so math.random can get the correct answer for the choosen question.