2017-11-01 20 views
-4

enter code here私はコードを実行可能にしていますが、動作していません。 ListOfQuestionsクラスがありますが、データがリストに入力されていません。そのクイズにはいくつか質問があり、それぞれに4つのオプションがあります。しかし、質問は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); 
     } 
    } 
} 
+1

は、コンソールは、質問がリストに挿入されていないことを示す印刷であるかの例を提供してもらえますか? –

+0

この 'public quiz(ListOfQuestionsクイズ){ this.quiz = quiz; } 'はconstrcutorのように見えますが、おそらくメソッドです - 戻り値の型は何ですか?このコードはコンパイルされますか? –

+0

クイズクラスの本体が見つかりません – jrook

答えて

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()); 
     } 
+0

さて、私はすでにそのコードの一部をコメントしました。今質問のリストは質問でいっぱいになっていませんし、toStringメソッドを使って出力しません。 –

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(); 
} 
} 

変更をクイズ変数を追加で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; 
} 

このコードの変更は、コードの実行可能な場合のみです。

+0

ありがとう、さて、それは動作します。今私は答えの選択肢とともに鍵を追加するという小さな変化をしなければならず、自分ではどのように考え出すのだろうか。再度ありがとう:) –

+0

あなたを助ける楽しみ – Anuradha

0

あなたtoString()方法はが含まれており、単純に返すように変更することができます。

return this.question + " " + this.getCorrectAnswer(); 
関連する問題