Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 存储字符串,然后用标签框显示_Vb.net_Replace - Fatal编程技术网

Vb.net 存储字符串,然后用标签框显示

Vb.net 存储字符串,然后用标签框显示,vb.net,replace,Vb.net,Replace,我试图存储输入框中的字符串,然后在类的刽子手游戏的lblbox中用破折号显示它 以下是我正在努力完成的任务: 编辑程序以允许任何长度的秘密字 该程序将允许“猜测者”猜测单词长度的2倍。例如,单词“code”将允许总共8次猜测 当用户猜测单词中包含的字母时,程序将: 计算用户已完成的尝试次数。 如果猜到正确的字母,则用正确的字母替换相应的破折号(-) 当所有的字母猜对后,所有的破折号(-)都应该替换为合适的字母,并且会出现一个消息框,说明“扮演刽子手干得好!” 如果用户无法在允许的猜测量内猜出正

我试图存储输入框中的字符串,然后在类的刽子手游戏的
lblbox
中用破折号显示它

以下是我正在努力完成的任务:

  • 编辑程序以允许任何长度的秘密字

  • 该程序将允许“猜测者”猜测单词长度的2倍。例如,单词“code”将允许总共8次猜测

  • 当用户猜测单词中包含的字母时,程序将:
    计算用户已完成的尝试次数。
    如果猜到正确的字母,则用正确的字母替换相应的破折号(-)

  • 当所有的字母猜对后,所有的破折号(-)都应该替换为合适的字母,并且会出现一个消息框,说明“扮演刽子手干得好!”

  • 如果用户无法在允许的猜测量内猜出正确的单词;破折号(-)应替换为游戏结束!这时会出现一个消息框,上面写着“对不起,正确的单词是”

  • 在第三个标签控件中显示所有不正确的字母将获得2分奖励

  • 如果用户两次猜到同一个不正确的字母,如果用户不允许,或将其计算在内,则将获得4分额外奖励积分

这是我的密码:

Dim strSecretWord As String
Dim strLetterGuessed As String
Dim blnDashReplaced As Boolean
Dim intNumberOfRemainingGuesses As Integer = 10
Dim intNumofGuesses As Integer = 0

lblSecretWord.Text = ""
lblNumberOfAttempts.Text = ""

'start game and have 1st user input a 5 letter word that 2nd player needs to guess
strSecretWord = InputBox("Please input a 5 letter word for user to guess:", "Please input secret word.").ToUpper


'displays five dashes for the secret word
lblSecretWord.Text = lblSecretWord.Text & "-----"


'guessing player recieves inputbox to make letter guesses 
MessageBox.Show("The length of the word is 5 letters, you will be given 10 guesses", "10 guesses", MessageBoxButtons.OK)
MessageBox.Show("Player who gets to guess, BE READY!", "Good Luck Guessing", MessageBoxButtons.OK)

'Counts number of attempts player gets (10) and replaces dashes with guessed letter if correct
'If guessed letter was incorrect, user loses a turn
For intNumberofGuesses = 1 To 10
    strLetterGuessed = InputBox("Please guess a letter:", "Letter Guess").ToUpper


    'Uses an IntIndex counter of 0 to 4 to execute 5 times (5 dashes)
    'Also uses the value of intIndex to check each of the 5 locations of the strSecretWord
    For intIndex As Integer = 0 To 4

        'if the user has guessed a correct letter then remove a dash and insert the correct letter guessed
        If strSecretWord.Substring(intIndex, 1) = strLetterGuessed Then
            lblSecretWord.Text = lblSecretWord.Text.Remove(intIndex, 1)
            lblSecretWord.Text = lblSecretWord.Text.Insert(intIndex, strLetterGuessed)
            blnDashReplaced = True
        End If
    Next intIndex

    'If the user guessed a correct letter on their last guess the blnDashReplaced is set and the true condition of the If statement is executed
    If blnDashReplaced = True Then

        'if there are no more dashes, and the game has been solved.
        If lblSecretWord.Text.Contains("-") = False Then
            MessageBox.Show("Great Job playign Hangman!", "Game Over", MessageBoxButtons.OK)
            lblRemainingNumberOfAttempts.Text = ""
            lblNumberOfAttempts.Text = ""
            Exit Sub
        Else
            blnDashReplaced = False
        End If
    Else

    End If
    lblNumberOfAttempts.Text = intNumberofGuesses
    intNumberOfRemainingGuesses = intNumberOfRemainingGuesses - 1
    lblRemainingNumberOfAttempts.Text = intNumberOfRemainingGuesses

Next
lblSecretWord.Text = "GAME OVER!"
MessageBox.Show("Better luck next time. Sorry the correct word was " & strSecretWord & ".", "You Lost", MessageBoxButtons.OK)
lblRemainingNumberOfAttempts.Text = ""
lblNumberOfAttempts.Text = ""

我添加了一个列表框来保存猜测的字母。其他意见和解释与此一致

Public Class Form3
    'Move this to a class level variable so it can be seen by
    'all the methods in the class
    Private strSecretWord As String

    Private Sub btnStartGame_Click(sender As Object, e As EventArgs) Handles btnStartGame.Click
        Dim strLetterGuessed As String
        Dim blnDashReplaced As Boolean
        Dim intNumberOfRemainingGuesses As Integer
        Dim intNumofGuesses As Integer = 0

        'Display correct number of dashes
        Dim numberOfDashes As Integer = strSecretWord.Length
        'Create a string with correct number of dashes
        'This uses and overload of the String constructor that takes a Char and an integer
        'as arguments and returns a string with that character repeated that number
        'of times. The lower case c following "-" indicates that - is a Char.
        Dim TotalNumofGuesses = numberOfDashes * 2
        lblRemainingNumberOfAttempts.Text = TotalNumofGuesses.ToString
        intNumberOfRemainingGuesses = TotalNumofGuesses
        Dim dashString As String = New String("-"c, numberOfDashes)
        'displays the dashes
        lblSecretWord.Text = dashString

        'guessing player recieves inputbox to make letter guesses 
        'You can use an Interpolated string to display variables in line surrounded by { }.
        'In older versions of VB String.Format() will yield the same result.
        MessageBox.Show($"The length of the word is {numberOfDashes} letters, you will be given {TotalNumofGuesses} guesses", $"{TotalNumofGuesses} guesses", MessageBoxButtons.OK)
        MessageBox.Show("Player who gets to guess, BE READY!", "Good Luck Guessing", MessageBoxButtons.OK)

        'Counts number of attempts player gets and replaces dashes with guessed letter if correct
        'If guessed letter was incorrect, user loses a turn
        For counter = 1 To TotalNumofGuesses
            strLetterGuessed = InputBox("Please guess a letter:", "Letter Guess").ToUpper
            'If lstLettersGuessed.Contains(strLetterGuessed) Then
            If lbxLettersGuessed.Items.Contains(strLetterGuessed) Then
                MessageBox.Show($"{strLetterGuessed} has already been guessed.", "Try Again")
                'need to do this so they are not cheated out of a guess
                TotalNumofGuesses += 1
                Continue For 'Moves to the next iteration of the For
            End If
            lbxLettersGuessed.Items.Add(strLetterGuessed)
            'lstLettersGuessed.Add(strLetterGuessed)
            'Uses an IntIndex counter of 0 to 4 to execute 5 times (5 dashes)
            'Also uses the value of intIndex to check each of the 5 locations of the strSecretWord
            For intIndex As Integer = 0 To numberOfDashes - 1
                'if the user has guessed a correct letter then remove a dash and insert the correct letter guessed
                If strSecretWord.Substring(intIndex, 1) = strLetterGuessed Then
                    lblSecretWord.Text = lblSecretWord.Text.Remove(intIndex, 1)
                    lblSecretWord.Text = lblSecretWord.Text.Insert(intIndex, strLetterGuessed)
                    blnDashReplaced = True
                End If
            Next intIndex

            'If the user guessed a correct letter on their last guess the blnDashReplaced is set and the true condition of the If statement is executed
            If blnDashReplaced = True Then
                'if there are no more dashes, and the game has been solved.
                If lblSecretWord.Text.Contains("-") = False Then
                    MessageBox.Show("Great Job playing Hangman!", "Game Over", MessageBoxButtons.OK)
                    'Do this at start of game, player wants to see final score
                    'lblRemainingNumberOfAttempts.Text = ""
                    'lblNumberOfAttempts.Text = ""
                    Exit Sub
                Else
                    blnDashReplaced = False
                End If
            End If
            'This is a shorter way of incrementing a variable
            intNumofGuesses += 1
            'Can't put an integer into a Text property, it needs a string
            lblNumberOfAttempts.Text = intNumofGuesses.ToString
            'This is a shorter way of decrementing a variable
            intNumberOfRemainingGuesses -= 1
            'Can't put an integer into a Text property, it needs a string
            lblRemainingNumberOfAttempts.Text = intNumberOfRemainingGuesses.ToString

        Next
        lblSecretWord.Text = "GAME OVER!"
        MessageBox.Show("Better luck next time. Sorry the correct word was " & strSecretWord & ".", "You Lost", MessageBoxButtons.OK)
        'Do this at start of game
        'lblRemainingNumberOfAttempts.Text = ""
        'lblNumberOfAttempts.Text = ""
    End Sub

    Private Sub btnSetUp_Click(sender As Object, e As EventArgs) Handles btnSetUp.Click
        lblSecretWord.Text = ""
        lblNumberOfAttempts.Text = "0"
        lblRemainingNumberOfAttempts.Text = "0"
        lbxLettersGuessed.Items.Clear()
        'start game and have 1st user input a 5 letter word that 2nd player needs to guess
        strSecretWord = InputBox("Please input a word for user to guess:", "Please input secret word.").ToUpper
    End Sub
End Class

好吧,你说你在挣扎。在这个特殊的例子中,你在努力解决什么具体的问题?什么事情没有按预期发生?发生了什么意想不到的事情?我们可以帮助您解决的问题是什么?第一部分是将用户键入的单词作为破折号,代码当前是为一个五个字母长的单词编写的。因此lbdisplay=“---”无效。我尝试使用此代码返回str.replace(/^[a-zA-Z]+$/g,'-');但节目对我大吼大叫。我如何看待用户输入的单词,让我们说单词是纸杯蛋糕。要在lbl中显示------(有7个破折号),您想知道输入字符串的长度并创建一个长度相同的破折号字符串吗?VB.net有一些内置的命令来实现这一点。然后,一些内置函数将允许您调整stirng(或者更好的是,子字符串)的中间位置,以正确猜测的适当字母替换“-”。是的。我已经看过我的课堂教程,它们解释了如何将字符串的部分xyz替换为123或修剪字符串的末端。但用户输入的每个章程都没有任何内容。我看了看是否有替代品,但没有看到任何东西在LBX上我很困惑因为我只有一个显示器LBL显示什么LBX我在这里遗漏了什么伙计们?Keps给我一条错误消息,字符串=(“单词长度为“&NumberOfDashs&”,您将得到“&TotalNumofGuesses&“chances”)MessageBox.Show(消息),“game details”,MessageBox Buttons.OK)lbx是列表框。这只是另一个控件。它更容易存储和显示使用过的字母。我的答案中没有“Dim message as String”。