Results 1 to 5 of 5

Thread: Competition Winner Announcement Program

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    27

    Lightbulb Competition Winner Announcement Program

    So I've been trying to do a program which asks for 5 competition participants name and 5 score mark as Input. And it should give output by announcing the winner with MsgBox("First Place goes to ...") and then announce the others with numeric order. I have no problem acquiring the inputs but I am having trouble how to write the outputs. Any ideas appreciated big time !






    Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim name1, name2, name3, name4, name5 As String
    Dim mark1, mark2, mark3, mark4, mark5 As Decimal

    name1 = TextBox1.Text
    name2 = TextBox2.Text
    name3 = TextBox3.Text
    name4 = TextBox4.Text
    name5 = TextBox5.Text

    Decimal.TryParse(InputBox("Enter in seconds"), mark1)
    Decimal.TryParse(InputBox("Enter in seconds"), mark2)
    Decimal.TryParse(InputBox("Enter in seconds"), mark3)
    Decimal.TryParse(InputBox("Enter in seconds"), mark4)
    Decimal.TryParse(InputBox("Enter in seconds"), mark5)





    End Sub

    End Class

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    27

    Re: Competition Winner Announcement Program

    Need help please I am desperate

  3. #3
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: Competition Winner Announcement Program

    Hi,

    The first thing to do is to create a Custom Class that can hold the associated information for each Contestant entered, being their Name and their Score. i.e:-

    Code:
    Private Class Contestant
      Public Property Name As String
      Public Property Score As Integer
    End Class
    Once done, you can then create an array of that custom class which can save the entered contestants and their scores. i.e:-

    Code:
    Dim myContestants(4) As Contestant
    You can then use a FOR loop to get the contestant information from the user. i.e:-

    Code:
    Dim CurrentContestant As New Contestant
     
    CurrentContestant.Name = InputBox("Enter Contestant Name", "Enter Name")
    Integer.TryParse(InputBox("Enter Contestant Score", "Enter Score"), CurrentContestant.Score)
    myContestants(ArrayElement) = CurrentContestant
    Once the information has been gathered from the user, you can then sort the information with the following LINQ and Lambda example. i.e:-

    Code:
    Dim myWinners() As Contestant = myContestants.OrderByDescending(Function(x) x.Score).ToArray
    All you need to do now is to create another FOR loop to display the winners in order. i.e:-

    Code:
    For Each WinnerInOrder As Contestant In myWinners
    Next
    Hope that helps.

    Cheers,

    Ian
    Last edited by IanRyder; Apr 16th, 2013 at 09:02 AM. Reason: Usual Typo's

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: Competition Winner Announcement Program

    Hello,

    A couple of suggestions. First off, do not use InputBox, use a TextBox instead, secondly, when using TryParse use it as it was intended to be used as shown below

    Code:
    Dim Score As Decimal = 0
    If Decimal.TryParse(txtScore.Text, Score) Then
        ' Use Score variable
    Else
        ' txtScore.Text does not contain a valid Decimal
    End If

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,419

    Re: Competition Winner Announcement Program

    here's my attempt:

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'textbox1 - textbox5 are for names
            Dim nameTextBoxes() As TextBox = {TextBox1, TextBox2, TextBox3, TextBox4, TextBox5}
            'textbox6 - textbox10 are for scores
            Dim scoreTextBoxes() As TextBox = {TextBox6, TextBox7, TextBox8, TextBox9, TextBox10}
    
            Dim names() As String = Array.ConvertAll(nameTextBoxes, Function(tb) tb.Text)
            Dim scores() As Decimal = Array.ConvertAll(scoreTextBoxes, Function(tb) CDec(Val(tb.Text)))
    
            'this sorts the scores + names concurrently, lowest score to highest score
            Array.Sort(scores, names)
            'this reverses the arrays so they are sorted highest score to lowest score
            Array.Reverse(scores)
            Array.Reverse(names)
    
            'output results
            For x As Integer = 1 To 5
                MsgBox(String.Format("In {0} place is {1} with {2} point{3}", getNumberWithSuffix(x), names(x - 1), scores(x - 1), If(scores(x - 1) <> 1, "s", "")))
            Next
    
        End Sub
    
        Private Function getNumberWithSuffix(ByVal number As Integer) As String
    
            Dim suffix As String = ""
    
            Select Case number
                Case 1
                    suffix = "st"
                Case 2
                    suffix = "nd"
                Case 3
                    suffix = "rd"
                Case Else
                    suffix = "th"
            End Select
    
            Return number.ToString & suffix
    
        End Function
    
    End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width