
Bernardo J.
asked 01/19/21Need help on control structures, arrays, and functions
Create a simple 5-question quiz with three (3) choices that will display the score of the user. The quiz should be about your favorite game/series. Make use of control structures, arrays, and functions in your Java program. Make sure the Interactive option is enabled to allow user input.
Sample question and choices:
- Who was Naruto's teacher at the Ninja Academy?
- Iruka
- Jiraiya
- Kakashi
1 Expert Answer

Patrick B. answered 01/19/21
Math and computer tutor/teacher
class GameQuizQuestion
{
protected String question;
protected String answers[];
protected int correctAnswer;
public GameQuizQuestion(String strQ, String A[],int n)
{
question = new String(strQ);
answers = new String[3];
for (int iLoop=0; iLoop<3; iLoop++)
{
answers[iLoop] = new String(A[iLoop]);
}
correctAnswer=n;
}
public String GetQuestion() { return(new String(question)); }
public String IndexerGetAnswerAtIndex(int iIndexPos)
{
String strReturn=null;
if ((iIndexPos>=0) && (iIndexPos<3))
{
strReturn = new String(answers[iIndexPos]);
}
return(strReturn);
}
public int GetCorrectAnswer() { return(correctAnswer); }
}
//------------------------------------------------------------------------
import java.io.*;
class GameQuiz
{
public void Go()
{
GameQuizQuestion questions[] = new GameQuizQuestion[5];
String answers[] = new String[3];
String question;
int answer;
question="When can Mario climb the ladder?";
answers[0] = "When he has the hammer";
answers[1] = "When the ladder is broken";
answers[2] = "When there is a barrel or fireball above";
answer=3;
questions[0]= new GameQuizQuestion(question,answers,answer);
question="How many rocks must Dig Dug Drop for the prize to appear?";
answers[0] = "4";
answers[1] = "2";
answers[2] = "3";
answer=2;
questions[1] = new GameQuizQuestion(question,answers,answer);
question="Where is TRON invincible during the TANKS screen?";
answers[0] = "Nowhere";
answers[1] = "In the DIAMOND in the center";
answers[2] = "Inside the MCP Cone";
answer=1;
questions[2] = new GameQuizQuestion(question,answers,answer);
question="Which of the following is NOT a factor that affects the \n"+
"behavior and speed of Evil Otto ?";
answers[0] = "the number of robots in the maze";
answers[1] = "the round number";
answers[2] = "the color of the robots";
answer=3;
questions[3] = new GameQuizQuestion(question,answers,answer);
question="Which of the follow characters will NOT persue the Burger Chef?";
answers[0] = "Hot Dog";
answers[1] = "Chicken";
answers[2] = "Pickle";
answer=2;
questions[4] = new GameQuizQuestion(question,answers,answer);
String inbuff;
Console console = System.console();
int score=0;
for (int iLoop=0; iLoop<5; iLoop++)
{
System.out.println("*****************************");
System.out.println(" Question # " + (iLoop+1) );
System.out.println("------------------------------");
System.out.println(questions[iLoop].GetQuestion() );
System.out.println("_______________________________\n");
for (int jLoop=0; jLoop<3; jLoop++)
{
System.out.println( "(" + (jLoop+1) + ")" + questions[iLoop].IndexerGetAnswerAtIndex(jLoop));
}
System.out.print(" Input your answer :>");
inbuff = console.readLine();
int userResponse = Integer.parseInt(inbuff);
if (userResponse==questions[iLoop].GetCorrectAnswer())
{
System.out.println(" That is correct !");
score++;
}
else
{
System.out.println("That is incorrect");
}
}
System.out.println(" Your score is " + score + " out of 5 ");
}
public static void main(String args[])
{
GameQuiz gameQuiz = new GameQuiz();
gameQuiz.Go();
}
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
source code uploaded to RESOURCES section under this link01/19/21