java domande risposta multipla

ht‮t‬ps://www.lautturi.com
java domande risposta multipla

To create a multiple choice quiz in Java, you can use a combination of loops and conditional statements to present the questions and check the answers.

Here's an example of how to do this:

import java.util.Scanner;

public class MultipleChoiceQuiz {
    public static void main(String[] args) {
        // Create a scanner to read input from the command line
        Scanner scanner = new Scanner(System.in);

        // Define the questions and answers
        String[] questions = {
            "What is the capital of France?",
            "What is the capital of Italy?",
            "What is the capital of Spain?"
        };
        String[][] answers = {
            {"Paris", "Rome", "Madrid"},
            {"Paris", "Rome", "Madrid"},
            {"Paris", "Rome", "Madrid"}
        };
        char[] correctAnswers = {'A', 'B', 'C'};

        // Initialize the score
        int score = 0;

        // Loop through the questions
        for (int i = 0; i < questions.length; i++) {
            // Display the question
            System.out.println(questions[i]);

            // Display the answer choices
            for (int j = 0; j < answers[i].length; j++) {
                System.out.println((char) ('A' + j) + ") " + answers[i][j]);
            }

            // Read the user's answer
            System.out.print("Enter your answer: ");
            char userAnswer = scanner.nextLine().charAt(0);

            // Check the answer
            if (userAnswer == correctAnswers[i]) {
                System.out.println("Correct!");
                score++;
            } else {
                System.out.println("Incorrect.");
            }
        }

        // Display the final score
        System.out.println("Your score: " + score + "/" + questions.length);
    }
}

This program will ask the user three multiple choice questions, check the answers, and display the final score at the end.

Created Time:2017-11-03 15:57:11  Author:lautturi