繁体   English   中英

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

[英]Quiz-How can i make questions which are saved in an array to be answered using 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();

        }
    }
}

我使用菜单来让用户选择他想做的事情。 我需要知道的是如何将数组放入方法的循环中。 任何帮助表示赞赏。

您可以使用while或for循环遍历数组。 在这里,我使用了for循环来打印测验问题并将用户响应存储在Answers数组中。

如果您对程序使用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();
    }
}

您需要一个for loop 因为索引很重要,所以不能使用增强的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--;
         }
     }
}

您的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