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


i want place array questions in loop in method.

  class quizmethods{     string [] questions = new string [20];     string [] answers   = new string [20];     string [] correctanswers = new string [20];     int score;      void fillarrays(){        questions[0] = " lead guitarist of led zeppelin?";      correctanswers[0] = "jimmy page";     questions[1] = " lead singer of led zeppelin?";     correctanswers[1]= "robert plant";     questions[2] = " lead guitarist of pink floyd?";      correctanswers[2] = "david gilmour";     questions[3] = " bassist , main lyricist of pink floyd?";     correctanswers[3]= "roger waters";     questions[4] = " under category of music pink floyd entitled too?";      correctanswers[4] = "soft rock";     questions[5] = " under category of music metallica entitled too?";     correctanswers[5]= "trash metal";     questions[6] = " lead guitarist of metallica?";      correctanswers[6] = "kirk hammet";     questions[7] = " lead singer , rhythm guitarist of metallica?";     correctanswers[7]= "james hetfield";     questions[8] = " lead guitarist of guns n roses?";      correctanswers[8] = "slash";     questions[9] = " lead singer of guns n roses?";     correctanswers[9]= "axl rose";     questions[13] = " under category of music guns n roses entitled too?";     correctanswers[13]= "hard rock";     questions[11] = " bassist of guns n roses?";     correctanswers[11]= "duff mckagan";     questions[12] = " name biggest , sold album of pink floyd";      correctanswers[12] = "the wall";     questions[13] = " under category of music zz top entitled too?";     correctanswers[13]= "blues rock";     questions[14] = " lead guitarist , vocalist of zz top?";      correctanswers[14] = "billy gibbons";     questions[15] = " considered king of blues?";     correctanswers[15]= "bb king";     questions[16] = " lead singer popular band queen?";      correctanswers[16] = "freddie mercury";     questions[17] = " lead singer heavy metal band black sabbath?";     correctanswers[17]= "ozzy osbourne";     questions[18] = " lead guitarist of dire straits?";      correctanswers[18] = "mark knopfler";     questions[19] = " complete sentence.alphaville released song named _______ _______ in september 1984.";     correctanswers[19]= "forever young"; }  void takequiz(){     // loop using arrays in 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 above");     byte menu = keyboard.readbyte();     switch(menu){          case 1 :          takequiz();          case 2 :         quizresults();          case 3 :         performancecomment();          case 4 :          exit();          }     } } 

i used menu user choose wants do. need know how can place array in loop in method. appreciated.

you need for loop. because index important, cannot use enhanced loops.

void takequiz() {      scanner in = new scanner(system.in);      string answer;       for(int = 0; < 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 needs breaks out of switch

switch(menu) {     case 1 :          takequiz();         break;     case 2 :         quizresults();         break;     case 3 :         performancecomment();         break;     case 4 :          exit();          break; // not need break if exit() used `system.exit(0)`     case default: // switch statements should include `default` declaration when no cases valid         exit();         break; } 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -