我在填充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
答案 0 :(得分:0)
我不是100%,但我很确定IsNothing正在检查是否有值。因为在HELLO设置之后你自动将数组分配给11个大小,但是“或”只是一个空的空间。所以试试这个
If inputArray(i) == "" Then
charArray(i) = "-1"
Else
charArray(i) = inputArray(i)
End If
答案 1 :(得分: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
答案 2 :(得分:0)
我能够通过首先创建char数组来实现我想要的,它将输入要猜的单词。然后创建一个字符串数组,一个具有char数组的值,另一个用于guess数组,用于比较第一个字符串数组。
这是解决这个问题的方法。