Private Sub Command1_Click()
Dim n As Integer,i As Integer,a As Long,b As Long,c As Long
a = 1
b = 1
n = Val(Text1.Text)
List1.AddItem Str(a)
List1.AddItem Str(b)
For i = 3 To n
List1.AddItem Str(c)
Next i
End Sub
上述程序中方框处可选语句为( )
①a=b ②c=a+b ③b=c
BMI指数 |
<18.522.9 |
18.5~23~ |
25~24.9 |
29.9 |
≥30 |
≥40.0 |
结果 |
偏瘦 |
正常 |
偏胖 |
肥胖 |
重度肥胖 |
极重度肥胖 |
程序运行时,在文本框Text1中输入体重,在文本框Text2中输入身高,单击”测试”按钮Command1,在标签Label4中显示对应的结果。程序运行界面如下图所示。
实现上述功能的VB程序如下,请回答下列问题:
Private Sub Command1_Click()
Dim h As Single, w As Single, ans As Single, s As String
w = Val(Text1.Text)
h = Val(Text2.Text)
①
If ans< 18.5 Then
s = “偏瘦”
ElseIf ans< 22.9 Then
s = “正常”
ElseIf ans<= 24.9 Then
s = “偏胖”
ElseIf ans<= 29.9 Then
s = “肥胖”
ElseIf ans< 40 Then
s = “重度肥胖”
②
s = “极重度肥胖”
End If
Label4.Caption = s
End Sub
① ②
Private Sub Command1_Click()
Dim n As Integer, p As Long, s As Double, i As Integer, t As Integer
s = 0
t = 1
p = 0
n = Val(Text1.Text)
For i = 1 To n
s = s + t * 1 / p
Next i
Label1.Caption = Str(s)
End Sub
程序运行时,在文本框Text1中输入一个整数,单击“判断”按钮Command1后,在标签Label3中显示判断结果。程序运行界面如下图所示。
Private Sub Command1_Click()
Dim x As Integer, h As Integer, k As Integer
x = Val(Text1.Text)
h = 0
k=1
Do While x > 0
h = h + x Mod 10
k=
x = x \ 10
Loop
IfThen
Label3.Caption = “Yes”
Else
Label3.Caption = “No”
End If
End Sub
Private Sub Command1_Click( )
Dim n As Integer,i As Integer,a As Integer,b As Integer
Randomize
n=Val(Text1.Text)
i=
Do While i<=n
a=Int(Rnd*100)
b=Int(Rnd*100)
IfThen
List1.AddItem“(“+Str(i)+”)“+Str(a)+”+“+Str(b)+”=”
i=i+1
End If
Loop
End Sub
编写一个验证哥德巴赫猜想的VB程序:程序运行时,在文本框Text1中输入一个大于等于4的偶数,单击“验证”按钮Command1后,如果哥德巴赫猜想验证成功,则在文本框Text2中显示“Yes”,并在列表框List1中显示用两个素数表示该偶数的等式,否则显示“No”。程序运行结果如图所示。
实现上述功能的VB代码如下,但加框处代码有错,请改正。
′函数pp(x)的功能是判断整数x是否为素数,若是素数则返回True,否则返回False
Function pp(x As Integer) As Boolean
Dim j As Integer
pp = True
j = 2
Do While j <= Int(Sqr(x)) And pp=True
If x Mod j = 0 Then pp = False Else
'①
Loop
End Function
Private Sub Command1_Click()
Dim n As Integer, p As Integer, q As Integer, pd As Boolean
n = Val(Text1.Text)
p = 1
pd = False
Do While Not pd And p < n
p = p + 1
'②
If pp(p) And pp(q) Then
Text2.Text = “Yes”
pd = True
List1.AddItem Str(n) + “=” + Str(p) + “+” + Str(q)
End If
Loop
If pd = False Then Text2.Text = “No”
End Sub
程序中加框①处应改正为;
加框②处应改正为。
算法思想:
首先生成一个比p大的全1数a,如果a能整除p,则算法结束,否则继续扩大a的位数,如此继续,直至a整除p。
程序运行界面如下图所示,在文本框Text1中输入整数p,单击“计算”按钮,在文本框Text2中输出整数s,在文本框Text3中输出乘积中1的个数。
小明依据上述描述设计如下VB程序。请回答下列问题:
Private Sub Command1_Click()
Dim p As Long, a As Long, b As Long, c As Long, n As Long
Dim s As String '存放整数s,因为该数较大,因此采用字符串存储
Do While True '判断输入的整数p是否为个位不是5的奇数
p = Val(Text1.Text)
If p Mod 2 < > 0 And p Mod 5 < > 0 Then Exit Do
Loop
a = 0
n = 0
Do While a < p
n = n + 1
Loop
c = a Mod p
s = Str(a \ p)
Do While c < > 0
a = c * 10 + 1
s = s & b
n = n + 1
Loop
Text2.Text = s
Text3.Text = Str(n)
End Sub