VB高手帮忙纠错

来源:百度知道 编辑:UC知道 时间:2024/07/04 03:31:29
在文本框中输入一行字符,判断其中字母、数字的个数
我编的是
Private Sub Command1_Click()
Text1.Text = ""
Text1.SetFocus
Command1.Enabled = False
End Sub

Private Sub Command2_Click()
s1 = 0
s2 = 0

If Text1.Text >= "a" And Text1.Text <= "z" Or ch >= "A" And Text1.Text <= Z Then
s1 = s1 + 1
ElseIf Text1.Text >= "0" And Text1.Text <= "9" Then
s2 = s2 + 1
End If
Print "字母个数为"; s1; "数字个数为"; s2

End Sub
为什么统计不出来?
我又修改了
s1 = 0
s2 = 0
For i = 1 To Len(Text1.Text)
If Text1.Text >= "a" And Text1.Text <= "z" Or ch >= "A" And Text1.Text <= Z Then
s1 = s1 + 1
ElseIf Text1.Text >= "0" And Text1.Text <= "9" Then
s2 = s2 + 1
End If
Next
Print "字母个数为"; s1; "数字个数为"; s2

现在可以统计了 但是现在情况是 比

都跟你说了要一个一个字符地来判断才可以!
Private Sub Command2_Click()
s1 = 0
s2 = 0
For i = 1 To Len(Text1.Text)
c = Mid(Text1, i, 1)
If c >= "a" And c <= "z" Or c >= "A" And c <= "Z" Then
s1 = s1 + 1
ElseIf c >= "0" And c <= "9" Then
s2 = s2 + 1
End If
Next
Print "字母个数为"; s1; "数字个数为"; s2
End Sub

建议你用ASCII码值判断

不能Text1.Text 整个判断
要一个一个字符地来判断才可以!

一:计算字符串的长度。
二:根据长度设置循环读取的次数
三:判断每个字符是那种类型,该类型的计数加1

Private Sub Command1_Click()
Text1.Text = ""
Text1.SetFocus
Command1.Enabled = False
End Sub

Private Sub Command2_Click()
Dim a As String
a = Len(Text1.Text)
Print a
End Sub