简体   繁体   English

测验-如何使问题保存在数组中,以便使用方法中的循环进行回答?

[英]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. 任何帮助表示赞赏。

You can loop over an array using while or for-loop. 您可以使用while或for循环遍历数组。 Here I have used the for-loop to print the quiz question and store the user response in answers array. 在这里,我使用了for循环来打印测验问题并将用户响应存储在Answers数组中。

Also it will be good if you use List interface implementations for your program. 如果您对程序使用List接口实现,那也将很好。

void takeQuiz(){

    Scanner keyboard = new Scanner(System.in);

    for(int i=0;i<questions.length;i++)
    {
        System.out.println(questions[i]);
        answers[i]= keyboard.nextLine();
    }
}

You need a for loop . 您需要一个for loop Because the index is important, you cannot use enhanced for loops. 因为索引很重要,所以不能使用增强的for循环。

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语句也需要breaks才能退出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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM