2017-01-30 27 views
0
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”,则第一个标签变为表示“J”。抱歉格式不佳。字符串中的对应字母

+0

'String.IndexOf'会告诉你某个字母或子字符串在哪里,但是在该代码中不是J已经在Letter1中显示? – Plutonix

+0

我忘了添加变量randomWord,它将包含“juicy”,在数组中循环并选择单词,所以单词可能不是多汁,它可以是“hello”或“goodbye”例如“因此它会显示j但它不会与其他词的工作。 – AESTHETIC

+0

会发生什么,如果池包含JUICY和班卓琴?你显示它在两个地方?'String.IndexOf'会告诉你,如果它包含一个字母,如果是的话,在这里 – Plutonix

回答

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