In some cases, users are often asked to answer some questions to do some interactive effects, such as questionnaire survey, question and answer, and so on. Today, I will share a way to extract questions in such cases. The realization of this demo is a problem not to repeat random function, namely the background 10 questions, every time the user to open the case, will be randomly selected from the background question lets users do a, if the right is from the remaining in the title and then randomly, until all the user wrong or right subject, the following is about the specific ideas and implementation methods.
I. Design ideas
1. Output the question numbers of all background questions to a one-dimensional array in the foreground when opening the case.
2. Randomly select a question number from the one-bit array and store it in a variable, then delete the question number from the one-dimensional array.
3. Go to the database to get the question number as the first question.
4. Repeat step 2 until the user answers the question incorrectly or completely.
Two. Implementation methods
1. The database
First of all, all questions are stored in the database, so we need to design the structure of the database first. In addition, we also need a question number for extraction, because the data ID will not be repeated, so we can directly use the data ID of each question as the question number.
According to the design idea, the operation of the database has two kinds of obtaining the question number and extracting the question, so we need two corresponding services. In the service of obtaining the item number, all the database is output first, and then the output result of the two-dimensional array mode is obtained. Because the data ID is the first column in the database, which is the corresponding column with the serial number 0 in the two-dimensional array, it can be taken as the return result of the service.
Front desk during initialization calls an initialization of the action group, group action of specific operation is to call to obtain qid services first, after finished the numerical variables “problem solving” the serial number and fraction set to 1, this variable is used to store the user answer the first question at the same time can also be used to calculate the user’s answer score, then there is service return the results to the front desk of a one-dimensional array “qid”, Randomly chosen from “qid” one element numerical variable output to “extract qid”, from “qid” this element will be deleted, then “qid” is the number of elements in the topic, the number of remaining to be assigned to the numerical variables “residual topic”, the last is to “extract qid” service to obtain the title, Assign the return result of the service to the foreground object array “topics”.
In the answer part, five text components are used to display questions and various options respectively. Here, data binding is made directly with the object array “questions” in the foreground, and the returned results can be directly displayed when the question service is obtained.
Click events are added to each of the four text components that display options. Here, the text component that displays option D is used as an example. When the user clicks, it will judge whether the answer stored in the object array “topic” is D. If yes, the action group will be executed. If not, the answer result will be displayed.
Pumping action group will first determine remaining questions whether to 0, if is that users have answered all the questions, then set the banner in customs clearance, according to the status and if it is not from a one-dimensional array “qid” to a random sample of a qid, and to obtain qid services to perform, then deleted from the “qid qid while the” residual topic “minus 1, The result of the service is then assigned to the object array “questions”, and finally the numeric variable “answer number and score” is incremented by 1.