Private Sub Command1_Click()
Dim sjzs As Integer
Dim rjzs As String
sjzs = Val(Text1.Text)
rjzs = ""
Do While Int(sjzs / 2) > 0
rjzs = Str(sjzs Mod 2) + rjzs
sjzs = Int(sjzs / 2)
Loop
Label1.Caption = rjzs
End Sub
例如:单词banana由b,a,n三个字母组成,其中a字母出现了3次,n字母出现了2次,b字母出现了1次,得到maxn为3,minn为1,maxn与minn的差为2,是一个素数,那么banana就是一个幸运单词。
编写一个判断幸运单词的VB程序,在文本框Text1中输入一个单词(注:仅输入小写字母,且长度小于100),单击“判断”按钮Command1,标签Labal1输出运行结果。
程序运行界面如图所示,实现上述功能的VB代码如下,但加框处代码有错,请改正。
Private Sub Command1_Click()
Dim maxn As Integer
Dim minn As Integer
Dim a(1 To 26)As Integer
maxn=0
minn=100
s= Text1.Text
n=Len(s)
For i=1 To n
dz=Mid(s,i,1)
If dz>=“a”And dz<=“z”Then
p= ‘(1)
a(p)=a(p)+1
End If
Next i
For j=1 To 26
If a(j)>0 Then
If a(j)>maxn Then maxn=a(j)
If a(i)<minn Then minn=a(j)
End If
Next j
If(maxn-minn>=1)And Then ‘(2)
Label1.Caption=“幸运单词!”
Else
Label1.Caption=“无!”
End If
End Sub
‘自定义函数iszs(x)用于判断整数x是否为素数,如果x是素数返回True
Function iszs(x As Integer)As Boolean
‘代码略
End Fuction
图a
图b |
图c |
Private Sub Command1_Click()
Dim s As String, ch As String
Dim fh As Integer, p As Integer, sum As Integer
s = Text1.Text: fh = 1: p = 0: sum = 0
For i = 1 To Len(s)
ch = Mid(s, i, 1)
If ① Then '判断取出的字符是否为数字
p = p * 10 + Val(ch)
Else
sum = sum + p * fh
p = 0
If ch = "-" Then
②
ElseIf ch = "+" Then
fh = 1
End If
End If
Next i
Label3.Caption = Str(sum)
List1.AddItem ③
End Sub
① ② ③
Private Sub Command1_Click()
Dim s1 As String, s2 As String, ch As String
s1 = Text1.Text
s2 =””
ch =””
For i = 1 To Len(s1)
ch = Mid (s1, i, 1)
If Asc(ch) <= 122 And Asc(ch) >= 97 Then
ch =Chr(Asc(ch) - 32)
End If
If Asc(ch) >= 65 And Asc(ch)<= 90 Then
ch = Chr(65 + (Asc(ch) - 62) Mod 26)
End If
s2 = s2 + ch
Next i
Text2.Text = s2
End Sub
已知大写字母A的ASCII码值为65,且小写字母的 ASCII 码为对应的大写字母的ASCII码加上32。运行上述程序,若文本框Text1 中的内容为“Text1”,单击命令按钮 Command1,则文本框Text2中将显示( )
Private Sub Command1_Click()
Dim t As Single, h1 As Single, h2 As Single
Const g = 9.8
t = Val(Text1.Text)
h1 = (g * t ^ 2) / 2
h2 = Int(h1)
Text2.Text = Str(h2)
End Sub
①
②
Private Sub Command1_Click()
Dim s As String, s1 As String, c As String
s = Text1.Text
For i = 1 To Len(s)
c = Mid(s, i, 1)
s1 = ⑴
For j = ⑵
If c <> Mid(s, j, 1) Then
s1 = s1 + Mid(s, j, 1)
End If
Next j
⑶
Next i
Text2.Text = s
End Sub
实现该功能的VB程序如下,但加框处代码有错,请改正。
Dim nam(100) As String '存放学生姓名
Dim sex(100) As String '存放学生性别
Dim math(100) As Long '存放学生数学成绩
Dim su(100) As Long '存放学生总分成绩
Dim k As Integer 'k变量用来存学生人数
Private Sub Form_Load()
‘将数据库中的数据分别存储在相应的变量中并在列表框中显示出来
‘代码略
End Sub
Private Sub Command1_Click()
Dim mmax As Integer, gmax As Integer, flagm As Boolean, flagg As Boolean
i = 1: flagm = True: flagg = True
Do While i <= k And flagm = True ①
mmax = 0: gmax = 0
flagm = False: flagg = False
For j = i To k - i + 1
If sex(j) = "男" And su(j) > su(mmax) Then mmax = j
If sex(j) = "女" And su(j) > su(gmax) Then gmax = j
Next j
If mmax <> 0 Then
flagm = True
t = nam(mmax): nam(mmax) = nam(i): nam(i) = t
t = sex(mmax): sex(mmax) = sex(i): sex(i) = t
t = math(mmax): math(mmax) = math(i): math(i) = t
t = su(mmax): su(mmax) = su(i): su(i) = t
End If
If gmax <> 0 Then
flagg = True
If j = mmax Then ②
gmax = mmax
t = nam(gmax): nam(gmax) = nam(k - i + 1): nam(k - i + 1) = t
t = sex(gmax): sex(gmax) = sex(k - i + 1): sex(k - i + 1) = t
t = math(gmax): math(gmax) = math(i): math(i) = t
t = su(gmax): su(gmax) = su(k - i + 1): su(k - i + 1) = t
Else
t = nam(gmax): nam(gmax) = nam(k - i + 1): nam(k - i + 1) = t
t = sex(gmax): sex(gmax) = sex(k - i + 1): sex(k - i + 1) = t
t = math(gmax): math(gmax) = math(k - i + 1): math(k - i + 1) = t
t = su(gmax): su(gmax) = su(k - i + 1): su(k - i + 1) = t
End If
End If
i = i + 1
Loop
For i = 1 To k
List2.AddItem nam(i) + " " + sex(i) + " " + Str(math(i)) + " " + Str(su(i))
Next i
End Sub
程序中①处改为。
程序中②处改为。
当n的值为10时,关于该两段程序,下列说法不正确的是( )
Private Sub Command1_Click()
Dim a As Integer,b As Integer
a=10 ‘①
b=20 ‘②
Text1.Text=“a+b=”+Val(a+b) ‘③
End Sub ‘④
发生“类型不匹配”错误时所执行的语句是( )
Dim i As Integer
Dim s As Integer
Dim n As Integer
n = 30
s = 0
For i = 2 To n - 1
If n Mod i = 0 Then s = s + i
Next i
Print s
程序运行后输出s的值是
Private Sub Command1_Click()
Dim a As Integer, b As Single
a = Val(Text1.Text)
b=3.14*a^2
Text2.Text = "圆面积为:"+ b
End Sub
m=5
sum=0
while m<10:
sum=sum+m
m=m+1
print(sum)
程序运行后输出的结果为:( )
for i in range (1,6):
print(‘*’)
s=0
while s<=29:
s=s+10 #循环体
print(s)
Const n = 6
Dim a(0 To n)As String
Dim i As Integer,j As Integer
a(1)="118":a(2)="36":a(3)="98":a(4)="15":a(5)="88":a(6)="2"
For i = 2 To 4
a(0)= a(i): j = i - 1
Do While a(0) < a(j)
a(j + 1)=a(j)
j =j - 1
Loop
a(j + 1)= a(0)
Next i
执行该段程序后,数组元素a(1)到a(6)的值依次为( )
for i in range(2, 6, 2)
a=i+1
print(a)
def xor_code(message,key):
①
for str in message:
pwd.append( ② ) #将字符逐个进行异或操作,得到结果
return.join(pwd)
message = input('请输入明文:')
pwd = xor_code(message,'A')
print(pwd)
① ②