【正确答案】文本框用Text属性来显示计算结果;命令按钮的标题由Caption属性来设置,单击命令按钮触发Click事件;为了检测单选按钮是否选中,可以通过检测Value属性来实现,当Value为True时,表示单选按钮被选中,否则未被选中。解题步骤如下。
(1)建立界面并设置控件属性。题目提供了程序用到的控件及其属性,如表11-4所示。
表11-4
|
控 件 | 属 性 | 设置值 |
单选按钮 | Name Caption | Op1 100—200之间素数 |
|
|
单选按钮 | Name Caption | Op2 200—400之间素数 |
|
|
文本框 | Name | Text1 |
命令按钮 | Name Caption | Cmd1 计算 |
|
|
命令按钮 | Name Caption | Cmd2 存盘 |
|
|
(2)编写程序代码。
程序提供的代码:
·标准模块代码
Option Explicit
Sub putdata(t_FileName As String,T_Str As Variant)
Dim sFile As String
sFile="/"&t_FileName
Open App.Path & sFile For Output As #1
Print #1,T_Str
Close #1
End Sub
Function isprime(t_I As Integer)As Boolean
Dim J As Integer
isprime=False
For J=2 To t_I/2
If t_I Mod J=0 Then Exit For
Next J
If J>t_I/2 Then isprime=True
End Function
窗体代码
Private Sub Cmd1_Click()
Dim i As Integer
Dim temp As Long
'temp=?
If Opt2.Value Then
For i=200 To 400
'If isprime(?)Then
temp=temp+1
End If
Next
Else
For i=100 To 200
If isprime(i)Then
temp=temp+i
End If
Next
End If
'Text1.?=temp
End Sub
Private Sub Cmd2_Click()
putdata"/out.txt",Text1.Text
End Sub
参考代码:
Private Sub Cmd1_Click()
Dim i As Integer
Dim temp As Long
temp=0
If Opt2.Value Then
For i=200 To 400
If isprime(i)Then
temp=temp+i
End If
Next
Else
For i=100 To 200
If isprime(i)Then
temp=temp+i
End If
Next
End If
Text1.Text=temp
End Sub
Private Sub Cmd2_Click()
putdata"/out.txt",Text1.Text
End Sub
(3)调试并运行程序。
(4)按题目要求存盘。