Create a interactive quiz using java
public class Question {
//two attributes
String prompt;
String answer;
//initialize method to initialize the object(give object intial value)
public Question(String prompt, String answer) { //accepting input
this.prompt = prompt;
this.answer = answer;
}
}
import java.util.Scanner;
public class App{
public static void main(String [] args) {
String q1 = "What is 1+1?\n"
+ "(a)1\n(B)2\n(c)3\n" ;
String q2 = "What is 2+1?\n"
+ "(a)0\n(B)2\n(c)3\n" ;
Question [] questions = { //create an array of question
new Question(q1, "b"), //q1 - prompt; a - answer
new Question(q2, "c")
};
takeTest(questions); //call the method
}
public static void takeTest(Question [] questions) { //accepting a parameter that is an array of question
int score = 0;
Scanner keyboardInput = new Scanner (System.in); //be able to get input from user
for(int i = 0; 1 < questions.length; i++) {
//print out question from array that we are looping through
System.out.println(questions[i].prompt); //in each interation through this loop, access a different question's prompt
String answer = keyboardInput.nextLine(); //create a variable to store user input as answer
//check if the answer is right
if(answer.equals(questions[1].answer)) { //compare the user's answer to the correct answer
score++;
}
}
System.out.println("You got " + score + "/" + questions.length);
}
}
// Static driver/tester method
static public void main(String[] args) {
new App(); // starting App object
}
App.main(null);