Display multiple choice questions in a random order

[亡魂溺海] 提交于 2020-01-24 22:56:05

问题


Below is the code that displays multiple choice questions sequentially. It displays a question, gets the answer and checks for correctness.

  public static void Rap() {
  Scanner input = new Scanner(System.in);
  int correct = 0;

  //1
  System.out.println("Complete the lyrics(hint travis scott): 'dont you open up that...  \n A.can of coke \n B.window \n C. back door \n D. water bottle ");
  String x = input.next();
  if (x.equalsIgnoreCase("b")) {
   System.out.println("Correct");
   correct++;
  }
  if (x.equalsIgnoreCase("a")) {
   System.out.println("incorrect");
  }
  if (x.equalsIgnoreCase("c")) {
   System.out.println("incorrect");
  }
  if (x.equalsIgnoreCase("d")) {
   System.out.println("incorrect");
  }
  //Same for other questions

  System.out.println("You got " + correct++ + "/15 good or bad job i guess");   

Please suggest how to randomize this flow?


回答1:


It looks like you are a beginner to programming, that's great!

From your code what I understand is: you have n number of questions displayed to the user and you want to keep count of correct answers.

Algorithmically,

for every question in n questions,
    display one question
    read the input
    if input is correct, increment the correct count
    display correct/incorrect
    go to next question

Solution: This answer might look complex, but trust me, you will learn a lot of things here.

First, create a class to store your question and correct answer's option.

class Question {
    /*Read about private access specifier and getter/setter methods*/
    String question;
    String correctOption;
    public Question(String question, String correctOption) {
        this.question = question;
        this.correctOption = correctOption;
    }
}

Secondly, create a list of these question objects (Read about array here and Lists here)

List<Question> allQuestions = new ArrayList<Question>();
allQuestions.add(new Question("YOUR_QUESTION", "CORRECT_OPTION"));
/*Example:*/
allQuestions.add(new Question("Complete the lyrics(hint travis scott): 'dont you open up that... \n A.can of coke \n B.window \n C. back door \n D. water bottle", "b"));
/* TODO do this for all the 15 questions*/

Thirdly, shuffle the Question objects in the allQuestions import java.util.Collections

Collections.shuffle(allQuestions);

Finally, follow the algorithm explained above:

//TODO prepare allQuestions as explained above
for (int i=0; i < allQuestions.size(); i++) {
    Question curQuest = allQuestions.get(i);
    System.out.println(curQuest.question)
    String ans = input.next();
    if(ans.equalsIgnoreCase(curQuest.correctOption)) {
        System.out.println("Correct");
        correct++;
    } else {
        System.out.println("incorrect");
    }
}
System.out.println("You got "+ correct++ +"/15 good or bad job i guess");


来源:https://stackoverflow.com/questions/44180643/display-multiple-choice-questions-in-a-random-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!