输出用户答案和方法中的正确答案" void quizResults()"

时间:2016-11-10 17:30:31

标签: java arrays

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(){
        String answer;
        int i;
        for (i = 0; i < 20; i++) {
            System.out.print(questions[i] + "\nAnswer: ");
            answer = Keyboard.readString();
            answers[i] = answer;

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

    void quizResults() {
    }

    void performanceComment() {
    }

    void exit() {
    }

    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.print("Choose from the above:");
        byte menu = Keyboard.readByte();
        switch(menu) {

        case 1: 
            takeQuiz();
            break;

        case 2:
            quizResults();
            break;

        case 3:
            performanceComment();
            break;

        case 4: 
            exit();        
        }
    }
}

我想在方法void quizResults()中输出用户答案和正确答案。我不知道该怎么做才能做到这一点。 如何以void exit()方法结束程序?

1 个答案:

答案 0 :(得分:0)

您已存储了用户的答案,并且您在数组中拥有正确的答案。 索引应该排成一行,这样你就可以循环遍历数组并输出到屏幕。

类似的东西:

public void quizResults(){
    for(int i = 0; i<20; i++){
        System.out.println("Question: #" + i);
        System.out.println("You answered: " + answers[i]);
        System.out.println("Correct answer: " + correctAnswers[i]);
}

我还建议您为switch语句设置默认大小写。如果用户输入1,2,3或4以外的内容,则需要以适当的方式处理(即表明他们输入了无效选项的消息。)