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

2015/1/10

How to make multiple choice quiz with string arrays?

Jamiah_DeShawn Jamiah_DeShawn

2015/1/10

#
Hello, I am trying to make a multiple choice quiz, with four possible answers using string arrays and am not too sure how to do so. I made classes for the four answers, the question, and I'm unsure of how to display the text on the string arrays in the world, and how to refer to these string arrays when displaying those questions in the strings. Also, how do I make it so that one of the four answers in the answer array are marked as correct, and are counted as so? can someone please help me out? for reference, my array in the world class looks like this:
1
2
3
4
public void Questionarray()
{
     String[] questionArray = {"Question 1", "Question 2", "Question 3",};
}
1
2
3
4
public void Answerarray()
{
    String[][] answerArray = {{"Answer1", "Answer2", "Answer3", "Answer4",}};
}
Thank you for looking at this and I hope you will be able to help me!
danpost danpost

2015/1/10

#
Jamiah_DeShawn wrote...
I made classes for the four answers the questions
What you gave in your code snippets are not classes; they are methods as written. The string arrays declared within them are local to the method and are immediately lost when the execution of the method ends. You need, first, to declare the arrays outside of any method for them to be retained for the life of the (I presume) world object that declares them. You can use a separate simple int array to hold the index of the correct answers. I am sure you will still have questions; but, next time, post the entire class code instead of snippets. This will allow us to see more clearly what you are doing and how we can rectify your issue(s) quickly.
Jamiah_DeShawn Jamiah_DeShawn

2015/1/10

#
Thank you for replying, can you please explain what you mean by declaring the arrays outside of a method and how you do it? The arrays are in my world class.
danpost danpost

2015/1/10

#
Just use line 3 alone in both snippets.
Jamiah_DeShawn Jamiah_DeShawn

2015/1/10

#
Thank you very much for giving me this advice! However, I'm not too sure how to utilize this string Array to print out the questions on the world. I'm not too sure how to print out the questions in my array in the world. Sorry for all the questions, I'm new to java and unsure about how to use arrays.
danpost danpost

2015/1/10

#
Maybe the java tutorial page on Arrays might help.
You need to login to post a reply.