Question: I'm currently working on this simple application that will create a quiz of ten questions and make it random. This is my progress so far,

I'm currently working on this simple application that will create a quiz of ten questions and make it random. This is my progress so far, the questions are random which what I need but the problem is the questions are repeating and I only need them to show once. Please help me to fix this bug.

NOTE: PLEASE SPECIFY WHERE SHOULD I PUT THE CODE NEEDED OR JUST SIMPLE ANSWER THEM WITH MY CODE. Thank you

activity_quiz.xml:

       

QuestionsLibrary.java:

public class QuestionsLibrary { public String mQuestions [] = { "Moses remains on the peak of Mt. Sinai for 40 days and 40 nights, then returns to the Israelites carrying what?", "From the burning bush, how does God reply to Moses when Moses asks for his name?", "Who was appointed as the first hereditary high priest of the Israelites??", "God and Moses make two new tablets. The third Commandment states that one should remember what?", "With the help of God and his staff, Moses leads the escape of the Israelites and parts which body of water?", "What river was Moses sent down by his mother?", "Where did Moses receive the Ten Commandments from God?", "How many plagues did God send to Egypt?", "Who is the Father of the Israelites?", "Which of the following was NOT one of the 10 plagues on Egypt?" }; private String mChoices [] [] = { {"Donkey", "The Ten Commandments", "Food", "Laptop"}, {"Time is Gold", "I think; therefore I am", "I Am that I Am", "Revive me Jett"}, {"Matthew", "Aaron", "Joshua", "Polo"}, {"The Sabbath day", "Valentine Day", "Holy Day", "Judgement Day"}, {"Red Sea", "Pacific Ocean", "Nile lake", "Caribbean Sea"}, {"The Nile", "The Jordan", "The Euphrates", "The Tigris"}, {"Mt. Olympus", "Mt. Diablo", "Mt. Nebo", "Mt. Sinai"}, {"12", "13", "8", "10"}, {"Joshua", "Moses", "Abraham", "Jesus"}, {"Frogs", "Flies", "Darkness", "Floods"} }; private String mCorrectAnswers [] = {"The Ten Commandments", "I Am that I Am", "Aaron", "The Sabbath day", "Red Sea","The Nile","Mt. Sinai","10","Abraham","Floods"}; public String getQuestion(int a) { String question = mQuestions[a]; return question; } public String getChoice1(int a) { String choice0 = mChoices[a] [0]; return choice0; } public String getChoice2(int a) { String choice1 = mChoices[a] [1]; return choice1; } public String getChoice3(int a) { String choice2 = mChoices[a] [2]; return choice2; } public String getChoice4(int a) { String choice3 = mChoices[a] [3]; return choice3; } public String getCorrectAnswer(int a) { String answer = mCorrectAnswers[a]; return answer; } }

QuizActivity.java:

import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import java.util.Random; public class QuizActivity extends AppCompatActivity { TextView mScoreView,mQuestionView; Button mButtonChoice1,mButtonChoice2,mButtonChoice3,mButtonChoice4,backQuiz; private QuestionsLibrary mQuestions = new QuestionsLibrary(); private String mAnswer; private int mScore = 0; private int num = 0; private int mQuestionsLenght = mQuestions.mQuestions.length; Random r; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView (R.layout.activity_quiz); r = new Random (); mScoreView = (TextView) findViewById (R.id.score); mQuestionView = (TextView) findViewById (R.id.question); mButtonChoice1 = (Button) findViewById (R.id.choice1); mButtonChoice2 = (Button) findViewById (R.id.choice2); mButtonChoice3 = (Button) findViewById (R.id.choice3); mButtonChoice4 = (Button) findViewById (R.id.choice4); backQuiz = (Button) findViewById (R.id.backQuiz); mScoreView.setText ("Score:" + mScore); updateQuestion (1+r.nextInt(mQuestionsLenght)); mButtonChoice1.setOnClickListener (new View.OnClickListener (){ @Override public void onClick(View v) { if (mButtonChoice1.getText () == mAnswer){ mScore = mScore + 1; mScoreView.setText ("Score:" + mScore); updateScore(mScore); updateQuestion (r.nextInt (mQuestionsLenght)); Toast.makeText (QuizActivity.this, "Correct", Toast.LENGTH_SHORT).show (); }else { Toast.makeText (QuizActivity.this, "Wrong", Toast.LENGTH_SHORT).show (); updateQuestion (r.nextInt (mQuestionsLenght)); } } }); mButtonChoice2.setOnClickListener (new View.OnClickListener (){ @Override public void onClick(View v) { if (mButtonChoice2.getText () == mAnswer){ mScore = mScore + 1; mScoreView.setText ("Score:" + mScore); updateScore(mScore); updateQuestion (r.nextInt (mQuestionsLenght)); Toast.makeText (QuizActivity.this, "Correct", Toast.LENGTH_SHORT).show (); }else { Toast.makeText (QuizActivity.this, "Wrong", Toast.LENGTH_SHORT).show (); updateQuestion (r.nextInt (mQuestionsLenght)); } } }); mButtonChoice3.setOnClickListener (new View.OnClickListener (){ @Override public void onClick(View v) { if (mButtonChoice3.getText () == mAnswer){ mScore = mScore + 1; mScoreView.setText ("Score:" + mScore); updateScore(mScore); updateQuestion (r.nextInt (mQuestionsLenght)); Toast.makeText (QuizActivity.this, "Correct", Toast.LENGTH_SHORT).show (); }else { Toast.makeText (QuizActivity.this, "Wrong", Toast.LENGTH_SHORT).show (); updateQuestion (r.nextInt (mQuestionsLenght)); } } }); mButtonChoice4.setOnClickListener (new View.OnClickListener (){ @Override public void onClick(View v) { if (mButtonChoice4.getText () == mAnswer){ mScore = mScore + 1; mScoreView.setText ("Score:" + mScore); updateScore(mScore); updateQuestion (r.nextInt (mQuestionsLenght)); Toast.makeText (QuizActivity.this, "Correct", Toast.LENGTH_SHORT).show (); }else { Toast.makeText (QuizActivity.this, "Wrong", Toast.LENGTH_SHORT).show (); updateQuestion (r.nextInt (mQuestionsLenght)); } } }); backQuiz.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(QuizActivity.this, tableofcontentActivity2.class); startActivity(i); } }); } private void updateQuestion(int num){ mQuestionView.setText (mQuestions.getQuestion (num)); mButtonChoice1.setText (mQuestions.getChoice1 (num)); mButtonChoice2.setText (mQuestions.getChoice2 (num)); mButtonChoice3.setText (mQuestions.getChoice3 (num)); mButtonChoice4.setText (mQuestions.getChoice4 (num)); mAnswer = mQuestions.getCorrectAnswer (num); } private void updateScore(int point) { mScoreView.setText ("" + mScore); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!