繁体   English   中英

Visual Basic,将值添加到空Char数组元素

[英]Visual Basic, Adding Values To Empty Char Array Elements

我无法在char数组中填充空数组元素。

我有一个inputArray ,它将把我为myInput输入的所有字符存储到数组中。 然后,我创建了一个charArray ,它将从inputArray中获取字符,然后,如果它在数组中什么都没有碰到,我想添加-1来表示charArray中的空位置。 我还没做到。 我输入“ Hello”,它将返回给我

H
E
L
L
O
Space
Space
Space
Space
And So On, until I reach the end of that array.
*"Space" is for visual aid of output

我迷路了。 我希望能够过滤掉“ -1”,然后计算需要组成该单词的下划线的数量,例如“ Hello”应为_ _ _ _ _(不包括带有“ -1”的元素或里面什么都没有。)

 Module GuessingGame
        Sub Main()
            Dim myInput As String.
            Dim inputArray(11) As Char
            Dim charArray(11) As Char
            Dim myLetter As String
            Dim attempts As Integer = 0
            Dim incorrectAttempts As Integer = 0
            Dim remainingAttempts As Integer = 0

            myInput = InputBox$("Please enter a word or phrase: ")
            inputArray = myInput.ToCharArray()

            While inputArray.Length > 12 
                System.Console.WriteLine("Error: The word or phrase needs to be 12 characters or less")
                myInput = InputBox$("Please enter a word or phrase: ")
                inputArray = myInput.ToCharArray()
            End While

            For i = 0 to inputArray.length - 1
                If IsNothing(inputArray(i)) Then
                    charArray(i) = "-1"
                Else
                    charArray(i) = inputArray(i)
                End If
            Next

            Console.Clear()

            For Each element In charArray
                System.Console.WriteLine(element)
            Next element

            For i = 0 to inputArray.length - 1
                remainingAttempts = remainingAttempts + 1
            Next

            remainingAttempts = remainingAttempts - 1

            System.Console.WriteLine("Guessing Game Display")
            System.Console.WriteLine("---------------------")
            System.Console.WriteLine("Number of Remaining Attempts: " & remainingAttempts)
            System.Console.WriteLine("Number of Incorrect Attempts: " & incorrectAttempts)
            System.Console.WriteLine("Number of Attempts: " & attempts)
            System.Console.WriteLine("Guess a Character? ")
            myLetter = InputBox$("Please enter a character: ")
        End Sub
    End Module

我不是100%,但是我很确定IsNothing正在检查是否有一个值。由于既然要在设置HELLO之后自动将数组的大小分配为11,所以“ “或”只是一个空白。 所以尝试这样做

If inputArray(i) == "" Then
                charArray(i) = "-1"
            Else
                charArray(i) = inputArray(i)
            End If

我认为您的代码工作正常,当integerArray中有空间时,它必须输出-1。 但是在下面的代码中,您必须对其进行修改:

While inputArray.Length > 12 
   System.Console.WriteLine("Error: The word or phrase needs to be 12 characters or less")
   myInput = InputBox$("Please enter a word or phrase: ")
   '**/*it is better to remove these two code  and replace it by goto above*/**
   inputArray = myInput.ToCharArray()
End While

通过首先创建char数组,我可以实现所需的功能,该数组将具有要猜测的单词的输入。 然后创建一个字符串数组,一个具有char数组中的值的字符串数组,一个用于猜测数组以比较第一个字符串数组的字符串数组。

这就是解决此问题的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM