代码积累,思维拓展
小李同学尝试使用不同方法提取字符串中的连续数字(连续数字算一个数)到数组中,并显示在text2中。程序运行界面如图所示:
Private Sub Command8_Click() ‘方法1累乘相加Dim s As String, i As Integer, p As Integer, ch As String Dim num As Integer
Dim flag As Boolean
s = Text1.Text
p = 1: flag = False: num = 0
For i = 1 To Len(s)
ch = Mid(s, i, 1)
If ch >= "0" And ch <= "9" Then
字符串截取数字部分flag = True
num = ___________________
Else
a(p) = num
If flag = True Then p = p + 1
__________________________
num = 0
End If
Next i
If num <> 0 Then a(p) = num: p = p + 1
Text2.Text = ""
For i = 1 To p - 1
Text2.Text = Text2.Text + Str(a(i))
Next i
End Sub
'方法2:数组直接累乘相加314陈雨婷
Private Sub Command10_Click()
s = Label1.Caption
p = 1: flag = False: num = 0
For i = 1 To Len(s)
ch = Mid(s, i, 1)
If ch >= "0" And ch <= "9" Then
a(p) = ______________________
ElseIf a(p) <> 0 Then
___________________________
End If
Next i
'处理最后一个字符是数字的情况
If a(p) <> 0 Then __________________ Text2.Text = ""
For i = 1 To p - 1
Text2.Text = Text2.Text + Str(a(i))
Next i
End Sub
‘方法3:子串截取法MID法,311符佳艺
Private Sub Command11_Click()
Dim p, j As Integer, s As String, ch As String
s = Text1.Text
p = 1: j = 0
For i = 1 To Len(s)
ch = Mid(s, i, 1)
If ch >= "0" And ch <= "9" Then
j = j + 1
ElseIf j > 0 Then
a(p) = _______________________
p = p + 1
____________________________
End If
Next i
'处理最后一个字符是数字的情况
If j > 0 Then a(p) = Val(Mid(s, i - j, j)): p = p + 1 Text2.Text = ""
For i = 1 To p - 1
Text2.Text = Text2.Text + Str(a(i))
Next i
End Sub
‘方法4:字符拼接法311程道友
Private Sub Command12_Click()
Dim p, i As Integer, s As String, ch As String
s = Text1.Text
p = 0
For i = 1 To Len(s)
If Mid(s, i, 1) >= "0" And Mid(s, i, 1) <= "9" Then ch =____________________________
Else
If ________________ Then
p = p + 1
a(p) = ________________
ch = ""
End If
End If
Next i
'处理数字在最后一个字符时的情况
If ch <> "" Then ____________: a(p) = Val(ch)
Text2.Text = ""
For i = 1 To p
Text2.Text = Text2.Text + Str(a(i))
Next i
End Sub

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。