This site requires JavaScript, please enable it in your browser!
Greenfoot back
elementa
elementa wrote ...

2014/12/15

How to create an array list for questions

elementa elementa

2014/12/15

#
I'm new to this whole Greenfoot thing but I'm interested in creating an array list for all 10 of my questions for my quiz game. How would I be able to do this efficiently? Also, if I wanted to compare the player's inputed answer with the correct answer, would I have to create a new array list for the answers or could I merge these array lists together?

IM DESPERATE PLEASE HELP

elementa elementa

2014/12/15

#
guys please help ~ i will be forever grateful THANK YOU
danpost danpost

2014/12/15

#
A List object is not needed for this. Since the number of question will be constant during the running of your scenario, an array would be sufficient. If your questions are in String form then you can define an array of type String in the class and assign the questions and answers to the array like this:
private String[][] questions =
{
    { "2+2", "4" },
    { "8-5", "3" },
    { "13-8", "5" }
};
When comparing the input, make sure you use the 'equals' method of the String class for comparison. Using the double equal sign '==' will compare to see if they are the same object; it will not compare the contents of the objects (the characters of the strings). This page of the Java tutorials will explain how to work with arrays.
elementa elementa

2014/12/15

#
Thanks so much ! But I have another question - how would you make it so that you could have more than one possible answer for a question and be able to compare the inputed text with the possible answers?
danpost danpost

2014/12/15

#
You can add a third or fourth string to any question block. You will have to iterate through the possible answers for possible matches:
boolean matchFound = false;
for (int i=1; i<questions[/** questionNumber */].length; i++)
{
    if (questions[/** questionNumber */][i].equals(/** guess */))
    {
        matchFound = true;
        break;
    }
}
if (matchFound)
// etc.
elementa elementa

2015/1/5

#
I have created a string array of 10 questions, and have done so according to the Java tutorial. However, Greenfoot highlights "public static void main(String args)" as "illegal static declaration in inner class Questions.QuestionsArrayList modifier 'static' is only allowed in constant variable declarations" Why is this?
davmac davmac

2015/1/5

#
elementa wrote...
illegal static declaration in inner class
I could guess that you've declared an inner class (probably without meaning to) and that your 'main' method is declared inside that inner class. Of course, guessing is my only option because you didn't post the code... ;)
elementa elementa

2015/1/6

#
@davmac : Oh, whoops ! Well, I'm not quite sure if this is the correct solution but I simply removed "static" so it becomes just "public void main(string args)" and I was able to compile it without a problem!
You need to login to post a reply.