2017-01-30 32 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