字符串中的对应字母

时间:2017-01-30 23:02:55

标签: vb.net

   Dim charArray() As Char = randomWord.ToCharArray     'Splits the selected words into individuals strings in a array

    Letter1.Text = charArray(0)                          ' Changes each label into its corresponding character from array
    Letter2.Text = charArray(1)                          '*
    Letter3.Text = charArray(2)                          '*
    Letter4.Text = charArray(3)                          '*
    Letter5.Text = charArray(4)                          '*

    If randomWord.Contains(answer) Then                  'if random word contains the users input
        MessageBox.Show("You are correct!")
        Correctcounter()
        winorLoss = "won"
        highScore()
        Userinput()

我设置了5个标签,每个标签对应一个5个字母的字符。如果用户输入(这是变量,我正在寻找一种方法)          答案)存在于变量randomWord中,变量randomWord中的相应字被设置为相应的Letter.text。因此,如果用户输入的是“j”,其出现在单词“juicy”中,则第一个标签变为表示“J”。抱歉格式不佳。

1 个答案:

答案 0 :(得分:0)

您没有向我们展示答案的数据类型。代码假设answerrandomWord类型为String

Dim i As Integer
Const COUNT_LIMIT As Integer = 5


If randomWord.Contains(answer) Then
    Dim charArray() As Char = randomWord.ToCharArray
    answer = answer

    Try
        For i = 1 To COUNT_LIMIT
            Dim lbl As Label = Me.Controls("Letter" & i.ToString)
            If Char.ToUpperInvariant(charArray(i - 1)) = Char.ToUpperInvariant(answer) Then
                lbl.Text = charArray(i - 1)
                'Exit For 'Use exit for if you are sure randomWord has no repeating character
            End If
        Next
    Catch ex As Exception
        Throw New Exception("Problem with label Letter" & i.ToString & " -- " & ex.Message)
    End Try

    MessageBox.Show("You are correct!")
    Correctcounter()
    winorLoss = "won"
    highScore()
    Userinput()
Else
    MessageBox.Show("Wrong! Try again.")
End If