Home > Enterprise >  Quiz with score
Quiz with score

Time:01-16

I don't know how and where to insert the score got by the user. Also, remove the repetition of asking another answer if they got it wrong. I'm kinda new to this so please help me. Thank you!

import java.util.Scanner;
public class GameQuiz {
        static final int QUESTION = 0;
        static final int ANSWER = 1;
        static final int CHOICES = 2;
            static String [][] QA = {
            {"1. Q1?", "A",
                "A. Option 1", "B. Option 2", "C. Option 3"},
            {"2. Q2?", "B",
                "A. Option 1", "B. Option 2", "C. Option 3"},
            {"3. Q3?", "C",
                "A. Option 1", "B. Option 2", "C. Option 3"},
            {"4. Q4?", "B",
                "A. Option 1", "B. Option 2", "C. Option 3"},
            {"5. Q5?", "A",
                "A. Option 1", "B. Option 2", "C. Option 3"}};

        public static void main(String args[]) {
            Scanner input = new Scanner(System.in);
                for (int QIndex = 0; QIndex < QA.length; QIndex  ) {
                    printQuestion(QIndex);
                do {
                    System.out.print("Your Answer: ");
                    } 
                while (!CorrectAnswer(QIndex, input.next().charAt(0)));
                }
        }
        static boolean CorrectAnswer(int QNum, char Answer) {
            boolean rightAnswer = (Answer   "").equalsIgnoreCase(QA[QNum][ANSWER]);
                System.out.println(rightAnswer ? "Correct!\n" : "Incorrect!");
                return rightAnswer;
        }

}

CodePudding user response:

As you want score as well, you can try something like below. Which will print final score as well.

A score variable has been defined and incremented for every correct answer.


import java.util.Scanner;

public class SquidGameQuiz {
    static final int QUESTION = 0;
    static final int ANSWER   = 1;
    static final int CHOICES  = 2;

    static String [][] QA = {
            {"1. Q1?", "A", "A. Option 1", "B. Option 2", "C. Option 3"},
            {"2. Q2?", "B", "A. Option 1", "B. Option 2", "C. Option 3"},
            {"3. Q3?", "C", "A. Option 1", "B. Option 2", "C. Option 3"},
            {"4. Q4?", "B", "A. Option 1", "B. Option 2", "C. Option 3"},
            {"5. Q5?", "A", "A. Option 1", "B. Option 2", "C. Option 3"}
    };

    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);

        int score = 0;

        for (int qIndex = 0; qIndex < QA.length; qIndex  ) {
            printQuestion(qIndex);
            if(correctAnswer(qIndex, input.next().charAt(0))){
                score = score   1;
            }
        }

        System.out.print("Final Score is : "   score);

    }

    static boolean correctAnswer(int QNum, char Answer) {
        boolean rightAnswer = (Answer   "").equalsIgnoreCase(QA[QNum][ANSWER]);
        System.out.println(rightAnswer ? "Correct!\n" : "Incorrect!");
        return rightAnswer;
    }

    static void printQuestion(int QNum) {
        System.out.println(QA[QNum][QUESTION]);

        int choices = QA[QNum].length;
        for (int x = CHOICES; x < choices; x  ) {
            System.out.println("\t"   QA[QNum][x]);
        }

        System.out.print("Your Answer: ");
    }
}

CodePudding user response:

with the answer from Renan L. You can fix your first problem so that your questions get shown in the console.

After that, you should add another int value that tracks the score. When you check if someone correctly answered the question in your CorrectAnswer function you can increase that score by one for example.

Here's an example:

import java.util.Scanner;
public class SquidGameQuiz  {
    static final int QUESTION = 0;
    static final int ANSWER = 1;
    static final int CHOICES = 2;
    static int SCORE = 0;

    static String [][] QA = {
            {"1. Q1?", "A",
                    "A. Option 1", "B. Option 2", "C. Option 3"},
            {"2. Q2?", "B",
                    "A. Option 1", "B. Option 2", "C. Option 3"},
            {"3. Q3?", "C",
                    "A. Option 1", "B. Option 2", "C. Option 3"},
            {"4. Q4?", "B",
                    "A. Option 1", "B. Option 2", "C. Option 3"},
            {"5. Q5?", "A",
                    "A. Option 1", "B. Option 2", "C. Option 3"}};

    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        for (int QIndex = 0; QIndex < QA.length; QIndex  ) {
            printQuestion(QIndex);
            System.out.print("Your Answer: ");
            CorrectAnswer(QIndex, input.next().charAt(0));
        }
        System.out.println("Finished. Your Score: "    SCORE);
    }
    static boolean CorrectAnswer(int QNum, char Answer) {
        boolean rightAnswer = (Answer   "").equalsIgnoreCase(QA[QNum][ANSWER]);
        System.out.println(rightAnswer ? "Correct!\n" : "Incorrect!");
        if(rightAnswer == true){ // this is new
            SCORE  ;
        }
        return rightAnswer;
    }
    static void printQuestion(int QNum) {
        System.out.println(QA[QNum][QUESTION]);
        int choices = QA[QNum].length;
        for (int x = CHOICES; x < choices; x  ) {
            System.out.println("\t"   QA[QNum][x]);;
        }
    }
}

CodePudding user response:

If I understand right, you would like to move to the next question if the answer is wrong as well.

Then just remove the do while block in the main function.

    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        for (int QIndex = 0; QIndex < QA.length; QIndex  ) {
            printQuestion(QIndex);

            System.out.print("Your Answer: ");

            CorrectAnswer(QIndex, input.next().charAt(0));
        }
    }
  •  Tags:  
  • Related