2

I have to write a quiz tool in java and i am stuck. I just want to create a question and fill it with answers. The answeres should be in the array "antworten".

MainQuiz.java class:

import java.lang.*;
public class MainQuiz {

    public static void main(String args[]){

        QuizFrage qf = new QuizFrage ("Welche Lebensmittel sind gesund?" ,
                 new QuizAntwort ("Apfel" ,"A" , true),
                 new QuizAntwort ("Chips", "B", false),
                 new QuizAntwort ("Orange" , "C", true),
                 new QuizAntwort ("Schokolade" , "D", false));
                qf.FrageStellen();
    }
}

QuizAntwort.java class:

public class QuizAntwort {
    protected String antwortxt;
    protected Boolean istrichtig;
    protected CharSequence antwortchr;



    public QuizAntwort(String string, String string2, boolean b) {
        // TODO Auto-generated constructor stub
    }




    public boolean checkAntwort(String gewaehlteAntworten) {
        if (gewaehlteAntworten.contains(antwortchr)) return true; else return false; 
    }
}

and QuizFrage.java class

public class QuizFrage {
private String fragentext;
private QuizAntwort antworten[];



public QuizFrage(String FrageString, QuizAntwort quizAntwort1,
        QuizAntwort quizAntwort2, QuizAntwort quizAntwort3,
        QuizAntwort quizAntwort4){

    fragentext = FrageString;

}



public void FrageStellen(){

    System.out.println(fragentext);
    for (QuizAntwort curantwort: antworten){
        System.out.println(curantwort.antwortchr + ": " + curantwort.antwortxt);
    }
} 
}

How do I fill the array "antworten" with quizantwort1,quizantwort2... ?

2 Answers 2

8

Use varargs:

public QuizFrage(String fragentext, QuizAntwort... antworten){
    this.fragentext = fragentext;
    this.antworten = antworten;
}
Sign up to request clarification or add additional context in comments.

Comments

3

Simple:

antworten = new QuizAntwort[] {quizAntwort1, quizAntwort2,
                               quizAntwort3, quizAntwort4};

1 Comment

@Puce That is true. But the whole structure of the code in the question has much to improve. Using varargs is not the primary problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.