在文本框中输入几个句子后,我需要通过单击按钮找到文本框中最长的句子。最长的句子应该显示在MessageBox中。
另一个按钮应该在MessageBox中显示句子的平均长度。
答案 0 :(得分:1)
好的,我是新手,但我会尽力帮忙!
我会考虑将文本框中输入的句子拆分为数组或列表,然后处理它。
Dim sentences As String()
sentences = TextBox1.Text.Split("?")
Dim avgSum As Double = 0
Dim avg As Integer = 0
Dim longest As Integer = 0
For Each sentence As String In sentences
If sentence.Length < longest Then longest = sentence.Length
avgSum = avgSum + sentence.Length
Next
avg = avgSum / sentences.Length
MsgBox("Average is: " + avg)
MsgBox("Longest is: " + longest)
答案 1 :(得分:0)
定义三个函数:第一个提取所有句子的函数,第二个确定最长句子,第三个确定平均句子长度。
提取所有句子
''' <summary>
''' Extracts all sentences from a text block. It is assumed that a sentence is terminated by either a period (.), a question mark (?), or an exclamation mark (!).
''' </summary>
''' <param name="text">The text block as string.</param>
''' <returns>An array of sentences.</returns>
''' <remarks></remarks>
Function sentences(ByVal text As String) As String()
Dim snts(Len(text)) As String
Dim curPeriodPos As Integer
Dim nextPeriodPos As Integer
Dim longestSnt As String
Dim nextSnt As String
Dim i As Integer
text = text + " "
text = Replace(text, "? ", ". ")
text = Replace(text, "! ", ". ")
curPeriodPos = InStr(text, ". ")
longestSnt = Mid(text, 1, curPeriodPos)
i = 0
Do While curPeriodPos + 1 < Len(text)
nextPeriodPos = InStr(curPeriodPos + 1, text, ". ")
nextSnt = Mid(text, curPeriodPos + 1, nextPeriodPos - curPeriodPos + 1)
snts(i) = nextSnt
i += 1
curPeriodPos = nextPeriodPos
Loop
Return snts
End Function
确定最长的句子
''' <summary>
''' Determines the longest sentence among an array of senetences.
''' </summary>
''' <param name="sentencesArray">A string array of sentences.</param>
''' <returns> A sentence as string.</returns>
''' <remarks></remarks>
Function longestSentence(ByVal sentencesArray() As String) As String
Dim longestSnt As String = ""
For Each sentence As String In sentencesArray
If Len(sentence) > Len(longestSnt) Then
longestSnt = sentence
End If
Next sentence
Return longestSnt
End Function
确定平均句子长度
''' <summary>
''' Determines the average sentence length of sentences in an array of sentences.
''' </summary>
''' <param name="sentencesArray">A string array of sentences.</param>
''' <returns>An integer representing the average sentence length.</returns>
''' <remarks></remarks>
Function averageSentenceLength(ByVal sentencesArray() As String) As Double
Dim sentenceLengths(sentencesArray.Length) As Integer
Dim total As Integer = 0
Dim average As Double
Dim counter As Integer = 0
For i As Integer = 0 To sentencesArray.Length - 1
sentenceLengths(i) = Len(sentencesArray(i))
If sentenceLengths(i) > 0 Then
total += sentenceLengths(i)
counter += 1
End If
Next i
average = CDbl(total) / CDbl(counter)
Return average
End Function
可以在按钮的事件处理程序中调用这些函数,如下所示:
Private Sub DisplayLongestSentence() Handles longestSententeceButton.Click
Dim text As String = TextBox1.Text
MessageBox.Show(longestSentence(sentences(text)))
End Sub
Private Sub DisplayAverageSentenceLength() Handles averageSententeceLengthButton.Click
Dim text As String = TextBox1.Text
MessageBox.Show(averageSentenceLength(sentences(text)))
End Sub