标签中文本的随机数

时间:2013-04-17 22:31:23

标签: vb.net

Public Class MainForm

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

Private Sub guestsTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles guestsTextBox.KeyPress
    ' allows only numbers and the Backspace key

    If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso
        e.KeyChar <> ControlChars.Back Then
        e.Handled = True
    End If
End Sub

Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'fills the list box and selects the first item

    typeListBox.Items.Add("Kid's Birthday")
    typeListBox.Items.Add("21st Birthday")
    typeListBox.Items.Add("40th Birthday")
    typeListBox.Items.Add("Other Birthday")
    typeListBox.SelectedIndex = 0
End Sub

Private Sub calcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles calcButton.Click
    'displays the total charge

    Dim guests As Integer
    Dim typeIndex As Integer
    Dim guestPrice As Integer
    Dim totalCharge As Integer

    Integer.TryParse(guestsTextBox.Text, guests)
    typeIndex = typeListBox.SelectedIndex

    'determine the price per guest
    Select Case typeIndex
        Case 0 'Kid's Birthday
            guestPrice = 11
        Case 1 '21st Birthday
            guestPrice = 20
        Case 2 '40th Birthday
            guestPrice = 25
        Case Else 'other birthdays
            guestPrice = 15
    End Select

    'calculate and display the total charge
    totalCharge = guests * guestPrice
    totalLabel.Text = totalCharge.ToString("C0")
End Sub

Private Sub testDataButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles testDataButton.Click
    Dim guests As Integer
    Dim typeIndex As Integer
    Dim guestPrice As Integer
    Dim totalCharge As Integer
    Dim randomGen As New Random
    Dim setCounter As Integer = 1

    testDataLabel.Text = String.Empty

    Do
        guests = randomGen.Next(1, 51)
        typeIndex = randomGen.Next(0, 4)

        For Each I As Object In typeListBox.SelectedItems
            testDataLabel.Text += I.ToString() & ControlChars.NewLine
        Next

        'determine the price per guest
        Select Case typeListBox.SelectedIndex
            Case 0
                guestPrice = 11
            Case 1
                guestPrice = 20
            Case 2
                guestPrice = 25
            Case Else
                guestPrice = 15
        End Select

        'calculate and display the total charge
        totalCharge = guests * guestPrice
        testDataLabel.Text = testDataLabel.Text &
            typeIndex.ToString & "    " &
            guests.ToString & "    " &
            totalCharge.ToString("C0") &
            ControlChars.NewLine
        setCounter += 1


    Loop Until setCounter > 10

End Sub

Private Sub typeListBox_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles typeListBox.SelectedIndexChanged

End Sub
End Class

当我点击名为“生成测试数据”的按钮时,它会生成标签中的随机数列表。我希望这些数字代表生日的类型而不是数字。

0是“孩子的生日”

1是“21岁生日”

2是“40岁生日”

和3是“其他生日”

我该怎么做呢?

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

如果我正确理解了您的问题,您可以声明一个枚举,并将该枚举的字典转换为String。

Enum负责处理代码中的数字,而不是使用人类可读的结构。字典将确保您的用户也将看到人类可读的结构。

请参阅下面的代码(需要一个全新的WinForms项目和主窗体上名为ListBox1的ListBox):

Option Strict On

Public Class Form1
  'Declare this enum to avoid dealing with naked numbers in code
  Enum BirthdayTypes
    btKids = 0
    bt21st = 1
    bt40th = 2
    btOther = 3
  End Enum

  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) _
                                                     Handles MyBase.Load
    'Suppose your input value is a number,
    'but you don't want to deal with numbers
    Dim typeIndex As Integer = 2

    'You can convert your number to BirthdayTypes,
    'making your input "correct"
    Dim typeIndexBt As BirthdayTypes = ConvertBirthdayIndexToType(typeIndex)

    'Calculation of guest price is now "human readable"
    Dim guestPrice As Integer = CalculateGuestPrice(typeIndexBt)

    'You can create a dictionary for diplaying the values
    Dim displayDictionary As New Dictionary(Of BirthdayTypes, String)
    With displayDictionary
      .Add(BirthdayTypes.btKids, "Kid's Birthday")
      .Add(BirthdayTypes.bt21st, "21st Birthday")
      .Add(BirthdayTypes.bt40th, "40th Birthday")
      .Add(BirthdayTypes.btOther, "Other Birthday")
    End With

    'Here is how you would those values into a ListBox
    With ListBox1
      .DataSource = displayDictionary.ToList
      .ValueMember = "Key"
      .DisplayMember = "Value"
    End With

    'Now your ListBox displays strings, 
    'but SelectedValue would return an object of type BirthdayTypes

    'You can extract random values from the above dictionary by index,
    'and create a new list from it

    Debug.WriteLine(ListBox1.SelectedValue) 'outputs btKids
  End Sub

  Private Function CalculateGuestPrice(bt As BirthdayTypes) As Integer
    Select Case bt
      Case BirthdayTypes.btKids : Return 11
      Case BirthdayTypes.bt21st : Return 20
      Case BirthdayTypes.bt40th : Return 25
      Case BirthdayTypes.btOther : Return 15
    End Select
    'should never here
    Throw New Exception("Unknown birthday type")
  End Function

  Private Function ConvertBirthdayIndexToType(index As Integer) As BirthdayTypes
    If index < 3 Then Return CType(index, BirthdayTypes)
    Return BirthdayTypes.btOther
  End Function
End Class

免责声明:此代码只是可以完成的演示的演示,并非意味着可以使用完整的解决方案。

答案 1 :(得分:0)

我会用一个字符串。

Dim thestring() As String = {"Kid's Birthday", _ 
                             "21st Birthday",  _
                             "40th Birthday",  _
                             "Other Birthday"}

现在每个数字都代表字符串中的文字。

thestring(0) = kids birthday
thestring(1) = 21 birthday
thestring(2) = 40th birthday
thestring(3) = other birthday

有意义吗?我会更进一步,但我不太了解你想要做的事情。