谁能告诉我错在那吗 vb编一子过程maxlength(s,max),在已知的字符串s中,找出最长的单词max,
PublicClassForm1Submaxlength(ByVals$,ByRefmax$)Dimn$,i%s=LTrim(s)i=InStr(s,"")DoWhile...
Public Class Form1
Sub maxlength(ByVal s$, ByRef max$)
Dim n$, i%
s = LTrim(s)
i = InStr(s, "")
Do While i <> 0
n = Mid(s, 1, i - 1)
If Len(n) > Len(max) Then max = n
s = Mid(s, i + 1)
i = InStr(s, "")
Loop
If Len(s) > Len(max) Then max = s
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim s, max As String
max = ""
s = TextBox1.Text
maxlength(s, max)
TextBox2.Text = max
End Sub
End Class 展开
Sub maxlength(ByVal s$, ByRef max$)
Dim n$, i%
s = LTrim(s)
i = InStr(s, "")
Do While i <> 0
n = Mid(s, 1, i - 1)
If Len(n) > Len(max) Then max = n
s = Mid(s, i + 1)
i = InStr(s, "")
Loop
If Len(s) > Len(max) Then max = s
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim s, max As String
max = ""
s = TextBox1.Text
maxlength(s, max)
TextBox2.Text = max
End Sub
End Class 展开
1个回答
展开全部
单词用空格分开,你的i = InStr(s, "") 这一句应该是 i = InStr(s, " ")
你的开发工具是vb.net的,可使用的函数是vb6的
给你一个测试过的vb.net代码
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim str1, str2 As String
str1 = TextBox2.Text
maxlength(str1, str2)
TextBox1.Text = str2
End Sub
Sub maxlength(ByVal ss1 As String, ByRef ss2 As String)
Dim str, str0 As String
str0 = ""
str = ""
If Not (ss1 Is Nothing) Then
Dim kk As Long
kk = ss1.IndexOfAny(" ")
While kk <> -1
str = ss1.Remove(kk, ss1.Length - kk)
ss1 = ss1.Remove(0, kk + 1)
kk = ss1.IndexOfAny(" ")
If str.Length > str0.Length Then
str0 = str
End If
End While
If ss1.Length > str0.Length Then
str0 = ss1
End If
ss2 = str0
End If
End Sub
你的开发工具是vb.net的,可使用的函数是vb6的
给你一个测试过的vb.net代码
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim str1, str2 As String
str1 = TextBox2.Text
maxlength(str1, str2)
TextBox1.Text = str2
End Sub
Sub maxlength(ByVal ss1 As String, ByRef ss2 As String)
Dim str, str0 As String
str0 = ""
str = ""
If Not (ss1 Is Nothing) Then
Dim kk As Long
kk = ss1.IndexOfAny(" ")
While kk <> -1
str = ss1.Remove(kk, ss1.Length - kk)
ss1 = ss1.Remove(0, kk + 1)
kk = ss1.IndexOfAny(" ")
If str.Length > str0.Length Then
str0 = str
End If
End While
If ss1.Length > str0.Length Then
str0 = ss1
End If
ss2 = str0
End If
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询