3 Replies - 550 Views - Last Post: 04 September 2012 - 10:15 AM Rate Topic: -----

#1 dcarp757  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 02-September 12

Array help

Posted 04 September 2012 - 09:27 AM

This is called "flag quiz". The user is shown a flag and from a drop down box submits the matching country. After 5 flags are shown the application ends. The flag is selected at random from an array. The tasks:

1. Add an instance variable numberCorrect and intialize it to 0.
2. Use the variable to count the number of correct answers submitted by the user. Increment the numberCorrect in the Submit buttons event handler.
3. Write a procedure DisplayMessage that displays a message in the scoreLabel box based on correct answers. 5 = Excellent, 4= Very Good, 3 = Good, 2 = Poor, 1or less = Fail.

Please see what I have below. I believe I have 1 and 2 correct. Please confirm. I have accomplished 3 but not using a procedure DisplayMessage as instructed. This is where I need help. How is task 3 coded?

Public Class FlagQuizForm

   ' String array stores country names
   Dim options As String() = New String() { _
      "Russia", "China", "United States", "Italy", _
      "Australia", "South Africa", "Brazil", "Spain"}

   ' Boolean array tracks displayed flags
   Dim used As Boolean() = _
      New Boolean(0 To options.GetUpperBound(0)) {}

   Dim count As Integer = 1 ' number of flags shown
   Dim country As String ' current flag's country

[b]   Dim numberCorrect As Integer = 0 ' Task 1[/b]

   ' handles Flag Quiz Form's Load event
   Private Sub FlagQuizForm_Load(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles MyBase.Load

      Array.Sort(options) ' alphabetize country names

      ' display country names in ComboBox
      optionsComboBox.DataSource = options

      DisplayFlag() ' display first flag in PictureBox
   End Sub ' FlagQuizForm_Load

   ' return full path name of image file as a String
   Function BuildPathName() As String

      ' begin with country name
      Dim output As String = country

      ' locate space character if there is one
      Dim space As Integer = output.IndexOf(" ")

      ' remove space from country name if there is one
      If space > 0 Then
         output = output.Remove(space, 1)
      End If

      output = output.ToLower() ' make characters lowercase
      output &= ".png" ' add file extension

      ' add path name
      output = output.Insert(0, _
         System.Environment.CurrentDirectory & "\images\")

      Return output ' return full path name
   End Function ' BuildPathName

   ' return an unused random number
   Function GetUniqueRandomNumber() As Integer

      Dim randomObject As Random = New Random()
      Dim randomNumber As Integer

      Do ' generate random numbers until unused flag is found
         randomNumber = randomObject.Next(0, used.Length)
      Loop Until used(randomNumber) = False

      ' indicate that flag has been used
      used(randomNumber) = True

      Return randomNumber ' return index for new flag
   End Function ' GetUniqueRandomNumber

   ' display random flag in PictureBox
   Sub DisplayFlag()

      ' unique index ensures that a flag is used no more than once
      Dim randomNumber As Integer = GetUniqueRandomNumber()

      ' retrieve country name from array options
      country = options(randomNumber)

      ' get image's full path name
      Dim path As String = BuildPathName()
      flagPicture.Image = Image.FromFile(path) ' display image
   End Sub ' DisplayFlag

   ' handles Submit Button's Click event
   Private Sub submitButton_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles submitButton.Click

      ' retrieve answer from ComboBox
      Dim response As String = _
         Convert.ToString(optionsComboBox.SelectedValue)

      ' verify answer
      If response = country Then
         feedBackLabel.Text = "Correct!"
     [b]    numberCorrect += 1 ' task 2[/b]

      Else
         feedbackLabel.Text = "Sorry, incorrect."
      End If


      ' inform user if quiz is over
      If count >= 5 Then ' quiz is over
         feedBackLabel.Text &= "  Done!"
         nextButton.Enabled = False
         submitButton.Enabled = False
         optionsComboBox.Enabled = False

      Else ' quiz is not over
         submitButton.Enabled = False
         nextButton.Enabled = True
      End If


    [b]  ' task 3 ??? (help)
      If count >= 5 AndAlso numberCorrect = 5 Then
         scoreLabel.Text = "Excellent!"
      ElseIf count >= 5 AndAlso numberCorrect = 4 Then
         scoreLabel.Text = "Very good"
      ElseIf count >= 5 AndAlso numberCorrect = 3 Then
         scoreLabel.Text = "Good"
      ElseIf count >= 5 AndAlso numberCorrect = 2 Then
         scoreLabel.Text = "Poor"
      ElseIf count >= 5 AndAlso numberCorrect <= 1 Then
         scoreLabel.Text = "Fail"
      End If[/b]



   End Sub ' submitButton_Click

   ' handles Next Flag Button's Click event
   Private Sub nextButton_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles nextButton.Click

      DisplayFlag() ' display next flag
      feedBackLabel.Text = "" ' clear output

      ' change selected country to first in ComboBox
      optionsComboBox.SelectedIndex = 0

      count += 1 ' update number of flags shown

      submitButton.Enabled = True
      nextButton.Enabled = False
   End Sub ' nextButton_Click
End Class ' FlagQuizForm




Is This A Good Question/Topic? 0
  • +

Replies To: Array help

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8363
  • View blog
  • Posts: 31,052
  • Joined: 12-June 08

Re: Array help

Posted 04 September 2012 - 09:41 AM

Quote

How is task 3 coded?

That's your homework and not our job to do. Go try some ideas out and see what happens.
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is online

  • Head MFIC
  • member icon

Reputation: 5060
  • View blog
  • Posts: 26,429
  • Joined: 10-May 07

Re: Array help

Posted 04 September 2012 - 09:44 AM

View Postmodi123_1, on 04 September 2012 - 12:41 PM, said:

Quote

How is task 3 coded?

That's your homework and not our job to do. Go try some ideas out and see what happens.

If it's required to be coded in VB6, perhaps step 1 is "Hop into time machine", & step 2 is "Set to when VB6 was a relevant development language".
Was This Post Helpful? 0
  • +
  • -

#4 dcarp757  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 02-September 12

Re: Array help

Posted 04 September 2012 - 10:15 AM

View Postmodi123_1, on 04 September 2012 - 09:41 AM, said:

Quote

How is task 3 coded?

That's your homework and not our job to do. Go try some ideas out and see what happens.


Understood, I have read the strict homework policy. Just looking for guidance. I figured it out.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1