enter code here
我正在尝试顶级使代码可行,但它不起作用。有一个类,ListOfQuestions但数据没有输入列表。它的测验应该有几个问题以及四个选项,每个问题都可以回答。但问题不会进入ListOfQuestions。
感谢您的帮助。
public class Main {
public static void main(String[] args) {
// write your code here
ListOfQuestions quiz1 = new ListOfQuestions();
Quiz quiz = new Quiz(quiz1);
quiz.add();
quiz.startQuiz();
}
}
import java.util.*;
import java.util.stream.Collectors;
public class Question {
private String question;
private Map<String, String> choices;
private String correctAnswer;
public Question(String question, String correctAnswer) {
this.question = question;
this.choices = new TreeMap<String, String>();
this.correctAnswer = correctAnswer;
//System.out.println(question);
}
public void addChoices(String option1, String option2, String option3, String option4) {
this.choices.put("A", option1);
this.choices.put("B", option2);
this.choices.put("C", option3);
this.choices.put("D", option4);
}
public String getCorrectAnswer() {
return this.correctAnswer;
}
public String getQuestion() {
return this.question;
}
public void enterChoice(String choice) {
if (choice.toUpperCase().equals(correctAnswer)) {
System.out.println("Bingo! your answer is correct");
} else {
System.out.println("Incorrect answer");
}
}
@Override
public String toString() {
return this.question;
}
/*public void print() {
Map<Letter, String> question = this.choices.entrySet().stream().sorted(Map.Entry.comparingByKey()).collect(Collectors.toMap(
Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap :: new));
question.forEach((k,v)->System.out.println(k + ": " + v));
}*/
}
class Quiz{
private ListOfQuestions quiz;
public Quiz(ListOfQuestions quiz) {
this.quiz = quiz;
}
public void add() throws ClassCastException{
try {
Question question1 = new Question("Question1", "C");
question1.addChoices("Answer1", "answer2", "answer3", "answer4");
this.quiz.addQuestion(question1);
Question question2 = new Question("question2", "B");
question2.addChoices("answer1", "answer2", "answer3",
"answer4");
this.quiz.addQuestion(question2);
} catch (Exception e) {
System.out.println("error");
}
}
public void startQuiz() {
this.quiz.pickQuestion();
}
}
import java.util.ArrayList;
import java.util.List;
public class ListOfQuestions {
private List<Question> questions;
public ListOfQuestions() {
this.questions = new ArrayList<Question>();
}
public void addQuestion(Question question) {
this.questions.add(question);
}
public void pickQuestion() {
for (Question question : this.questions) {
System.out.println(question);
}
}
}
答案 0 :(得分:1)
问题在于Question.toString()
方法。迭代Map.entrySet()
时,它返回Map.Entry (combination of key and value)
而不是条目中的键。
示例:
for (Map.Entry<String, String> key : this.choices.entrySet()) {
System.out.println(key.getKey() + " : " + key.getValue());
}
答案 1 :(得分:0)
这里我只更改了可行的代码
将测验变量添加到类测验,如下所示,
public class Quiz {
ListOfQuestions quiz;
public Quiz(ListOfQuestions quiz) {
this.quiz = quiz;
}
public void add() throws ClassCastException{
try {
Question question1 = new Question("Question1", "C");
question1.addChoices("Answer1", "answer2", "answer3", "answer4");
this.quiz.addQuestion(question1);
Question question2 = new Question("question2", "B");
question2.addChoices("answer1", "answer2", "answer3",
"answer4");
this.quiz.addQuestion(question2);
} catch (Exception e) {
System.out.println("error");
}
}
public void startQuiz() {
this.quiz.pickQuestion();
}
}
更改Question类中的toString方法,如下所示,
@Override
public String toString() {
String sentence = "";
for (Map.Entry<String, String> key : this.choices.entrySet()) {
sentence += this.choices.get(key.getKey());
}
return this.question;
}
此代码更改仅适用于您的代码。
答案 2 :(得分:0)
您的toString()
方法包含suspicious call
,只需修改即可返回:
return this.question + " " + this.getCorrectAnswer();