下列程序用来判断一个单词是否是素单词:设定小写字母a-z对应的数字为1-26,大写字母A-Z对应的数字为27-52,将某个单词的所有字母按照其对应的数字求和,判断其和是否是素数,如果是输出“It is a prime word.”,反之则输出“It is not a prime word.”。如单词“Hello”,字母分别对应的数字为:34、5、12、12、15,其和为34+5+12+12+15=78,不是素数,因此单词“Hello”并不是素单词,在Label1中输出“It is not a prime word.”。(已知字母A的ASCII码值为65,a的ASCII码值为97)。程序运行界面如下图所示:
Private Sub Command1_Click( )
Dim s As String, t As String, n As Integer
Dim i As Integer, m As Integer
s = Text1.Text
n = Len(s)
For i = 1 To n
t = Mid(s,i,1)
If t >= "A" And t <= "Z" Then
m = ①
ElseIf t >= "a" And t <= "z" Then
m= m + Asc(t)- 96 ‘Asc(t)用于返回字符t的ASCII 码值
End If
Next i
For i = 2 To m - 1
If Then Exit For
Next i
If ② Then
Label1.Caption = "It is a prime word."
Else
Label1.Caption = "It is not a prime word."
End If
End Sub