If ArrySentence.Contains(InputString) Then
Dim index As Integer = Array.IndexOf(ArrySentence, InputString)
Do Until a >= ArrySentence.GetUpperBound(0) + 1
Dim position As Integer = index + 1
index = Array.IndexOf(ArrySentence, InputString, position)
Console.WriteLine("The word ""{0}"" is at position {1}.", InputString, position)
a = a + 1
Loop几乎完成了,但问题是输出重复了一些行,如:
请输入一个句子
苹果从苹果树上掉下来
苹果这个词位于第二位。
苹果这个词位于第六位。
苹果这个词位于第二位。
苹果这个词位于第六位。
帮助!
发布于 2016-08-24 00:03:24
我不太明白你的循环背后的道理。您要做的是:搜索数组中的单词。如果它存在,打印它的位置。否则退出。重新开始。
我会这样做:
Dim index As Integer = Array.IndexOf(ArrySentence, InputString)
While index >= 0 'while the word has been found
'Output the occurrence
Console.WriteLine("The word ""{0}"" is at position {1}.", InputString, index + 1)
'Search for the next occurrence
index = Array.IndexOf(ArrySentence, InputString, index + 1)
End While发布于 2016-08-24 00:09:10
试试这个,对不起,但我没有想知道你做的那个有什么问题。
Dim sentence as String = "The apple fall from the apple tree"
Dim words As String() = sentence.Split(New Char() {" "c})
For i = 0 To words.Length - 1
If InputString = words(i) Then
Console.WriteLine("The word ""{0}"" is at position {1}.", InputString, i + 1)
End If
Nexthttps://stackoverflow.com/questions/39112192
复制相似问题