How to implement multiple choice quiz

walterf25

Expert
Licensed User
Longtime User
Hello everyone, I was wondering if i could get some tips from all the experts in here, i need to develop a quiz type app, which will ask a few questions, and each question will have three option buttons, only one will be the correct answer, how can i implement this? i'm randomly generating the questions, from 1 to 100, but i need to find a way to extract the correct answer ans throw in two more answers but only one will be the correct one, can anyone here maybe help me figure this out please?

thanks,
Walter
 

MDEnt

Member
Licensed User
Longtime User
Well what I'm looking to possibly do is randomize the answer position (not value) if possible, so the order always changes.

For example, I place the choices on a label like:

question.text = QuestionList.Get(Qnumber)&CRLF&CRLF&"A. "&AnswerList1.Get(Qnumber)&CRLF&"B. "&AnswerList2.Get(Qnumber)&CRLF&"C. "&AnswerList3.Get(Qnumber)

I do have a random number being generated ok already into values called answerA,answerB,answerC - but wondering of the easiest (or even possible) way to use those values to swap the answer list positions so if AnswerList1 is currently choice A - next time around with the same question that answer may be choice B or C - so the 3 possible answers are jumbled for every question every time a person takes the quiz.

So in thought at this moment - somewhat like:

question.text = QuestionList.Get(Qnumber)&CRLF&CRLF&"A. "&AnswerList(answerA).Get(Qnumber)...

Where answerA may hold a 1,2 or 3.
 
Last edited:
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Randomizing AnswersList

Hi there, the example i provided on the previous page, does exactly what you are trying to accomplish, not only it randomizes the answer but also the position of the answer!!!!!

cheers,
Walter
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Here you are.
B4X:
Sub Process_Globals
    Dim ImageFiles(4) As String
    ImageFiles(0) = "Rose.jpg"
    ImageFiles(1) = "Gentiane.jpg"
    ImageFiles(2) = "LysMartagon.jpg"
    ImageFiles(3) = "Edelweiss.jpg"
End Sub

Sub Globals
    Dim ImageView1 As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("main")
    ImageView1.Bitmap = LoadBitmap(File.DirAssets, ImageFiles(0))
End Sub

Sub ImageView1_Click
    Dim i As Int
  
    i = Rnd(0, 4)
    ImageView1.Bitmap = LoadBitmap(File.DirAssets, ImageFiles(i))
End Sub
Attached the small test project.

Best regards.
 

Attachments

  • ImageFileArray.zip
    153.3 KB · Views: 318
Upvote 0

djveleno

Active Member
Licensed User
Longtime User
Here you are.
B4X:
Sub Process_Globals
    Dim ImageFiles(4) As String
    ImageFiles(0) = "Rose.jpg"
    ImageFiles(1) = "Gentiane.jpg"
    ImageFiles(2) = "LysMartagon.jpg"
    ImageFiles(3) = "Edelweiss.jpg"
End Sub

Sub Globals
    Dim ImageView1 As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("main")
    ImageView1.Bitmap = LoadBitmap(File.DirAssets, ImageFiles(0))
End Sub

Sub ImageView1_Click
    Dim i As Int
 
    i = Rnd(0, 4)
    ImageView1.Bitmap = LoadBitmap(File.DirAssets, ImageFiles(i))
End Sub
Attached the small test project.

Best regards.

If instead wanted to show a picture after the other, and not random?
Thanks
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Use a counter:
B4X:
Sub Process_Globals
    Dim ImageFiles(4) As String
    ImageFiles(0) = "Rose.jpg"
    ImageFiles(1) = "Gentiane.jpg"
    ImageFiles(2) = "LysMartagon.jpg"
    ImageFiles(3) = "Edelweiss.jpg"
    Dim Counter = 0 As Int
End Sub

Sub Globals
    Dim ImageView1 As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("main")
    ImageView1.Bitmap = LoadBitmap(File.DirAssets, ImageFiles(Counter))
End Sub

Sub ImageView1_Click
    ImageView1.Bitmap = LoadBitmap(File.DirAssets, ImageFiles(Counter))
    Counter = Counter + 1
    If Counter > 3 Then
        Counter = 0
    End If
End Sub
Best regards.
 
Upvote 0

djveleno

Active Member
Licensed User
Longtime User
Use a counter:
B4X:
Sub Process_Globals
    Dim ImageFiles(4) As String
    ImageFiles(0) = "Rose.jpg"
    ImageFiles(1) = "Gentiane.jpg"
    ImageFiles(2) = "LysMartagon.jpg"
    ImageFiles(3) = "Edelweiss.jpg"
    Dim Counter = 0 As Int
End Sub

Sub Globals
    Dim ImageView1 As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("main")
    ImageView1.Bitmap = LoadBitmap(File.DirAssets, ImageFiles(Counter))
End Sub

Sub ImageView1_Click
    ImageView1.Bitmap = LoadBitmap(File.DirAssets, ImageFiles(Counter))
    Counter = Counter + 1
    If Counter > 3 Then
        Counter = 0
    End If
End Sub
Best regards.

Thank Klaus,
In the attached example, fully functional, with the code you sent me previously.:rolleyes:
 

Attachments

  • ImageQuiz.zip
    163.1 KB · Views: 345
Upvote 0

brianwiz12

Active Member
Licensed User
Longtime User
Attached you find your project a little bit modified.
In the database you entered systematically 0,1,2,0,1,2 etc in the ansind column.
The index in this column should be the index of the correct answer !
If Answer1 is the correct one then ansind = 0
If Answer2 is the correct one then ansind = 1
If Answer3 is the correct one then ansind = 2

in the rbtAnswer_CheckedChange routine we can check if the RadioButton that raised the event contains the correct answer.

Best regards.


Hello Klaus,

I used your example and edited to my own database that has 4 records. I keep seeing the same two over and over. I have included my the zip file. The database that is included works perfect... I made my own and edited the scope and something is off

It will not let me export from the file.. Keeps getting me an error... Will add once I find instructions here is what I believe the code problem lies for me atleast

B4X:
Dim Result As Int

    Counter = Counter + 1
    Qnumber = Rnd(1, 3)
    lblQuestion.Text = QuestionList.Get(Qnumber)
    If Qnumber >= 3 Then
        Qnumber = 0
    End If
    'If Counter = 10 Then
        'Result = Msgbox2("You have answered 8 correct out of 10", "Score", "Continue", "Finish", "", Null)
        'Counter = 0
    'End If
    rbtAnswer(0).Checked = False
    rbtAnswer(1).Checked = False
    rbtAnswer(2).Checked = False
    rbtAnswer(0).Text = AnswerList1.Get(Qnumber)
    rbtAnswer(1).Text = AnswerList2.Get(Qnumber)
    rbtAnswer(2).Text = AnswerList3.Get(Qnumber)

When I try to zip the file I get the attached error message

Update 1010pm est time: When I use my database and import the questions table it works...properly.

11pm est time update

B4X:
xSQL.Initialize(File.DirDefaultExternal, "football.db", True)

    xCursor = xSQL.ExecQuery("SELECT * FROM Questions")

    For i = 0 To xCursor.RowCount - 1
        xCursor.Position = i
        QuestionList.Add(xCursor.GetString("Question"))
        AnswerList1.Add(xCursor.GetString("Answer1"))
        AnswerList2.Add(xCursor.GetString("Answer2"))
        AnswerList3.Add(xCursor.GetString("Answer3"))
        RightIndex.Add(xCursor.GetInt("ansind"))
    Next
    xCursor.Close

    btnNext_Click
End Sub

Sub btnNext_Click
    Dim Result As Int

    Counter = Counter + 1
    Qnumber = Rnd(1, 95)
    lblQuestion.Text = QuestionList.Get(Qnumber)
    If Qnumber >= 95 Then
        Qnumber = 0
    End If

The above works fine

Then when I switch it to my table with 6 questions code
B4X:
xSQL.Initialize(File.DirDefaultExternal, "football.db", True)

    xCursor = xSQL.ExecQuery("SELECT * FROM Quiz")

    For i = 0 To xCursor.RowCount - 1
        xCursor.Position = i
        QuestionList.Add(xCursor.GetString("Question"))
        AnswerList1.Add(xCursor.GetString("Answer1"))
        AnswerList2.Add(xCursor.GetString("Answer2"))
        AnswerList3.Add(xCursor.GetString("Answer3"))
        RightIndex.Add(xCursor.GetInt("ansind"))
    Next
    xCursor.Close

    btnNext_Click
End Sub

Sub btnNext_Click
    Dim Result As Int

    Counter = Counter + 1
    Qnumber = Rnd(1, 6)
    lblQuestion.Text = QuestionList.Get(Qnumber)
    If Qnumber >= 6 Then
        Qnumber = 0
    End If

All it does it cycle between three different questions. I have to answer them twice to go to next question if it even works
 

Attachments

  • errormessage.jpg
    errormessage.jpg
    8.4 KB · Views: 191
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
Attached you have a modified version with Logs to see what happens.
To get the question number you must avoid to ask a same question several times.
You could set up a list with randomly distribured question numbers.
 

Attachments

  • test1.zip
    248.9 KB · Views: 288
Upvote 0

brianwiz12

Active Member
Licensed User
Longtime User
Thank you for the reply and step through. I was really confused how one database setup just like the original database didn't work.

Also I love these boards... the amount ideas I have and things I find like... Background images... Had a thought about it but in VB and not in the app. Now I see how much more it can look better

Believe it or not you guys make me a better programmer and help the drive to do good ones and not one off's
 
Upvote 0
Top