2017-01-30 9 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として設定される方法を探しています。したがって、ユーザの入力が単語「juicy」に存在する「j」である場合、第1のラベルは「J」を表すように変化する。貧弱な書式設定には申し訳ありません。文字列内の対応する文字

+0

'String.IndexOf'は特定の文字または部分文字列がどこにあるかを示しますが、そのコードではすでにLetter1に表示されていませんか? – Plutonix

+0

"juicy"を含む変数randomWordを追加するのを忘れてしまい、配列を循環して単語を選ぶので、単語が "hello"や "goodbye"といったジューシーなものではないかもしれない " – AESTHETIC

+0

プールにJUICYとBANJOが含まれているとどうなりますか?2か所で表示しますか?文字列が含まれている場合は 'String.IndexOf'が表示されます。もしそうなら、 – Plutonix

答えて

0

回答のデータタイプは表示されませんでした。以下のコードはanswerrandomWordStringであると仮定しています。

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 
関連する問題