1 Replies - 6023 Views - Last Post: 07 August 2012 - 05:55 AM Rate Topic: -----

#1 Coolai21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 07-August 12

Quiz Program Visual Basic 2010

Posted 07 August 2012 - 01:16 AM

Sorry for the disturbance but can you help me in my problem?

I'm new at visual basic programming and everything was fine until our topic shifted to arrays. I tried to understand it's code using Java. (Example: method are called functions.. .)

My prof has given us an exercise to create a Quiz program that asks the user more than 5 questions (in textbox) with choices (in buttons) and computes the score at the end (All just in one form). If the user click an a button it will tell if it's right or wrong and then proceed to change the question along with the choices.

*Required: - After the user finish the quiz the score will be displayed and there should be a restart button and all the question will be asked again randomly no pattern. - Try to make functions.

I tried searching the web since yesterday and I still have made no progress at my code.

Public Class Form1
    Dim questions(5) As String
    Dim answers(5) As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Method/Function for loading the Q&A
        loadQsAndAs()
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Me.Close()
    End Sub

    Private Sub loadQsAndAs()
        'Questions
        questions(0) = "What is 1 + 1?"
        questions(1) = "Who is the first man to walk on the Moon?"
        questions(2) = "What is the name of the main character in the movie: Yes Man!(2007)"
        questions(3) = "If I gave you three apples and you ate two, how many is left?"
        questions(4) = "What do you want in your final grade?"
        questions(5) = "What is the name of the thing(s) that you use whenever you eat?"
        'Answers
        answers(0) = "2"
        answers(1) = "Neil Armstrong"
        answers(2) = "Jim Carrey"
        answers(3) = "1"
        answers(4) = "A 4.0"
        answers(5) = "A Spoon and Fork"

        TextBox1.Text = setTheQuestion()
        Button1.Text = setTheAnswer1()
        Button2.Text = setTheAnswer2()
        Button3.Text = setTheAnswer3()
        Button4.Text = setTheAnswer4()

    End Sub

    Private Function setTheQuestion() As String
        Dim randomValue As New Random
        Dim randomQ As String = ""
        Dim i As Integer
        Dim index As Integer

        For i = 0 To 0
            index = randomValue.Next(0, questions.Length)
            randomQ &= questions(index)
        Next
        Return randomQ
    End Function

    Private Function setTheAnswer1() As String
        Dim randomValue As New Random
        Dim randomAns As String = ""
        Dim i As Integer
        Dim index As Integer

        For i = 0 To 0
            index = randomValue.Next(0, answers.Length)
            randomAns &= answers(index)
        Next

        Return randomAns
    End Function

    Private Function setTheAnswer2() As String
        Dim randomValue As New Random
        Dim randomAns As String = ""
        Dim i As Integer
        Dim index As Integer

        For i = 0 To 0
            index = randomValue.Next(1, answers.Length)
            randomAns &= answers(index)
        Next

        Return randomAns
    End Function

    Private Function setTheAnswer3() As String
        Dim randomValue As New Random
        Dim randomAns As String = ""
        Dim i As Integer
        Dim index As Integer

        For i = 0 To 0
            index = randomValue.Next(2, answers.Length)
            randomAns &= answers(index)
        Next

        Return randomAns
    End Function

    Private Function setTheAnswer4() As String
        Dim randomValue As New Random
        Dim randomAns As String = ""
        Dim i As Integer
        Dim index As Integer

        For i = 0 To 0
            index = randomValue.Next(3, answers.Length)
            randomAns &= answers(index)
        Next

        Return randomAns
    End Function

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        loadQsAndAs()
    End Sub
End Class


My questions are:

How do I make the questions and my choices in an array to appear randomly whenever the user starts or restarts the program?

How do I code that the random question will also have the correct answer with the other 3 random answers? (Because mine is just randomizing the answer and it keeps repeating some choices)

Can anyone help me?

Is This A Good Question/Topic? 0
  • +

Replies To: Quiz Program Visual Basic 2010

#2 torind_2000  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 55
  • Posts: 285
  • Joined: 22-August 11

Re: Quiz Program Visual Basic 2010

Posted 07 August 2012 - 05:55 AM

ForumLeader Edit:- Removal of quoted post

I'd think you need to pass the result of the question function to the answer function so you can match them up.
Otherwise you'll get randomly assigned values.
You could use an if statement/Select case in your
Private Function setTheQuestion() As String

that once you generate your question it checks the value and assigns the answer . Then you wouldn't need an answer funtion.
some pseudo code

Private Function setTheQuestion() As String 
Generate random number based on question list 
if randomNum = 3
 rightAnswer = corresponding value from answer list
pass value to some variable



That's probably what I would do, but someone way more experienced will give you a hint on the right way to do it soon enough!
Good luck, post your answer once you figure it out please.

This post has been edited by AdamSpeight2008: 07 August 2012 - 06:53 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1