Dim n As Integer
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
n = 0
Dim s As String
s = Text1.Text
For i = 1 To Len(s)
If Val(Mid(s, i, 1)) Mod 2 <> 0 Then
n = n + 1
End If
Next i
Text2.Text = n
End If
End Sub
追问还有个不是正整数的情况没有
追答Dim n As Integer
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
If Val(Text1.Text) > 0 Then
n = 0
Dim s As String
s = Text1.Text
For i = 1 To Len(s)
If Val(Mid(s, i, 1)) Mod 2 <> 0 Then
n = n + 1
End If
Next i
Text2.Text = n
End If
Else
MsgBox "输入的不是一个正整数,请检查", vbOKOnly
End If
End Sub
本回答被网友采纳