Natadad Natadad - 23 days ago 14
Java Question

Quiz-How can i make questions which are saved in an array to be answered using a loop in a method?

I want to place the array questions in a loop in a method.

class QuizMethods{
String [] questions = new String [20];
String [] answers = new String [20];
String [] correctAnswers = new String [20];
int score;
void fillArrays(){

questions[0] = " Who is the lead guitarist of Led Zeppelin?";
correctAnswers[0] = "Jimmy Page";

questions[1] = " Who is the lead singer of Led Zeppelin?";
correctAnswers[1]= "Robert Plant";

questions[2] = " Who is the lead guitarist of Pink Floyd?";
correctAnswers[2] = "David Gilmour";

questions[3] = " Who is the bassist and main lyricist of Pink Floyd?";
correctAnswers[3]= "Roger Waters";

questions[4] = " Under which category of music are Pink Floyd entitled too?";
correctAnswers[4] = "Soft Rock";

questions[5] = " Under which category of music are Metallica entitled too?";
correctAnswers[5]= "Trash Metal";

questions[6] = " Who is the lead guitarist of Metallica?";
correctAnswers[6] = "Kirk Hammet";

questions[7] = " Who is the lead singer and rhythm guitarist of Metallica?";
correctAnswers[7]= "James Hetfield";

questions[8] = " Who is the lead guitarist of Guns n Roses?";
correctAnswers[8] = "Slash";

questions[9] = " Who is the lead singer of Guns n Roses?";
correctAnswers[9]= "Axl Rose";

questions[13] = " Under which category of music are Guns n Roses entitled too?";
correctAnswers[13]= "Hard Rock";

questions[11] = " Who is the bassist of Guns n Roses?";
correctAnswers[11]= "Duff McKagan";

questions[12] = " Name the biggest and most sold album of Pink Floyd";
correctAnswers[12] = "The Wall";

questions[13] = " Under which category of music are ZZ Top entitled too?";
correctAnswers[13]= "Blues Rock";

questions[14] = " Who is the lead guitarist and vocalist of ZZ Top?";
correctAnswers[14] = "Billy Gibbons";

questions[15] = " Who is considered as the king of blues?";
correctAnswers[15]= "BB King";

questions[16] = " Who was the lead singer for the popular band Queen?";
correctAnswers[16] = "Freddie Mercury";

questions[17] = " Who was the lead singer for the Heavy Metal band Black Sabbath?";
correctAnswers[17]= "Ozzy Osbourne";

questions[18] = " Who is the lead guitarist of Dire Straits?";
correctAnswers[18] = "Mark Knopfler";

questions[19] = " Complete the sentence.Alphaville released the song named _______ _______ in September 1984.";
correctAnswers[19]= "Forever Young";
}
void takeQuiz(){
// loop using arrays in this method.
}
void menu(){
System.out.println("1. Take Quiz");
System.out.println("2. Quiz Results");
System.out.println("3. Performance Comment");
System.out.println("4. Exit");
System.out.println("Choose from the above");
byte menu = Keyboard.readByte();
switch(menu){

case 1 :
takeQuiz();

case 2 :
quizResults();

case 3 :
performanceComment();

case 4 :
exit();

}
}
}


I used the menu for the user to choose what he wants to do.
All I need to know is how can I place the array in a loop in the method.
Any help is appreciated.

Answer

You need a for loop. Because the index is important, you cannot use enhanced for loops.

void takeQuiz()
{
     Scanner in = new Scanner(System.in);
     String answer;

     for(int i = 0; i < questions.size(); i++)
     {
         System.out.print(questions[i] + "\nAnswer: ");
         answer = in.next();
         answers[i] = answer;

         if(answer.equalsIgnoreCase(correctAnswers[i])
         {
             score++;
         }
         else
         {
            score--;
         }
     }
}

Your switch statement also needs breaks to get out of the switch

switch(menu)
{
    case 1 : 
        takeQuiz();
        break;
    case 2 :
        quizResults();
        break;
    case 3 :
        performanceComment();
        break;
    case 4 : 
        exit(); 
        break; // Do not need break if exit() used `System.exit(0)`
    case default: // Switch statements should include a `default` declaration for what to do when no cases are valid
        exit();
        break;
}
Comments