1

我在填充 char 数组中的空数组元素时遇到问题。

我有一个inputArray,它将我为myInput键入的所有字符存储到一个数组中。然后我创建了一个charArray,它将从inputArray中获取字符,然后如果它在数组中没有遇到任何内容,我想添加 -1 以表示我的charArray中的空位置。我没能做到这一点。我输入“你好”,它会输出给我

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
4

3 回答 3

0

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

If inputArray(i) == "" Then
                charArray(i) = "-1"
            Else
                charArray(i) = inputArray(i)
            End If
于 2013-10-27T05:47:03.367 回答
0

我认为您的代码工作正常,当 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
于 2013-10-27T05:50:04.343 回答
0

我能够通过首先创建 char 数组来实现我想要的,该数组将输入要猜测的单词。然后制作字符串数组,一个包含 char 数组中的值,一个用于猜测数组以比较第一个字符串数组。

这是解决此问题的方法。

于 2013-11-20T02:56:23.960 回答