1、1 题目:请按照题目要求设计指定的窗体。 -输入一个正整数数,判断其是否为素数,并用 msgbox 来显示结果。例如输入 23 显示23 是素数;输入 21 则显示21 不是素数。判断按钮:实现素数判断Option ExplicitPrivate Sub Command1_Click()Dim x As Integer x 用来表示正整数Dim i As Integer i 用来作为循环变量x = Val(Text1.Text)For i = 2 To x - 1If x Mod i = 0 Then Exit ForNext iIf i x - 1 ThenMsgBox x & “是素数“E
2、lseMsgBox x & “不是素数“End IfEnd Sub2 题目:请按照题目要求设计指定的窗体。-输入一个二进制数,将其转化为十进制数。转换按钮:用于实现进制的转换清空按钮:用于实现文本框的清空,同时将焦点定位在 Text1 中Option ExplicitPrivate Sub Command1_Click()Dim s As String 用来表示 2 进制数Dim x As Integer 用来表示转化后的 10 进制Dim i As Integer 用来表示循环变量s = Text1.TextFor i = 1 To Len(s)x = x + Mid(s, i, 1) *
3、2 (Len(s) - i)Next iText2.Text = xEnd Sub3 题目:请按照题目要求设计指定的窗体。-输入一个 10 进制正整数,将其转换为一个 2 进制数。转换按钮:实现进制转换清空按钮:实现文本框的清空,同时焦点定位在 Text1 中Option ExplicitPrivate Sub Command1_Click()Dim x As Integer 用来表示 10 进制数Dim s As String 用来表示 2 进制数x = Val(Text1.Text)Dos = (x Mod 2) & sx = x 2Loop Until x = 0Text2.Text =
4、 sEnd Sub4 题目:请按照题目要求设计指定的窗体。-输入 2 个正整数,求这 2 个数的最大公约数。 Option Explicit计算按钮: 用来实现计算最大公约数 Private Sub Command1_Click()退出按钮:用来实现程序的退出 Dim m As Integer, n As IntegerDim r As Integer m = Val(Text1.Text)n = Val(Text2.Text)Dor = m Mod nm = nn = r Loop Until r = 0 Text3.Text = mEnd Sub5 题目:请按照题目要求设计指定的窗体。-产
5、生 10 个 10100 之间的两位随机整数,将 10 个数显示一个文本框中同时查找其中是否有某个数,如果有给出其在数组中的位置,如果没有给出相应的提示信息。结果信息用 msgbox 消息框弹出提示,如“36 出现在第 2 位” ;或者“没有出现 36”Option Explicit For i = 1 To 10Dim a(1 To 10) As Integer If x = a(i) Then Exit ForPrivate Sub Command1_Click() Next iDim i As Integer If i 10 ThenMsgBox “没有出现“ & xFor i = 1
6、To 10 Elsea(i) = Int(Rnd * 90 + 10) MsgBox x & “出现在第“ & i & “位“Text1.Text = Text1.Text & a(i) & “ “ End IfNext I End SubEnd SubPrivate Sub Command2_Click()Dim i As Integer Dim x As Integer x = Val(Text2.Text)6. 题目:请按照题目要求设计指定的窗体。-随机产生 10 个两位整数,存放在数组中,将 10 个数显示在一个文本框中并从中找出一个最大数和一个最小数,分别显示在文本框中。Option
7、 ExplicitDim a(1 To 10) As Integer Private Sub Command1_Click()Dim i As Integer For i = 1 To 10a(i) = Int(Rnd * 90 + 10)Text1.Text = Text1.Text & a(i) & “ “Next iEnd SubPrivate Sub Command2_Click()Dim i As Integer Dim max As Integer, min As Integer max = 0min = 100For i = 1 To 10If max a(i) Then min
8、 = a(i)Next iText2.Text = maxText3.Text = minEnd Sub7 题目:请按照题目要求设计指定的窗体。-随机产生 10 个两位整数并将 10 个数采用选择排序的算法实现升序排列Option ExplicitDim a(1 To 10) As Integer Private Sub Command1_Click()Dim i As Integer For i = 1 To 10a(i) = Int(Rnd * 90 + 10)Text1.Text = Text1.Text & a(i) & “ “Next iEnd SubPrivate Sub Comm
9、and2_Click()Dim i As Integer Dim j As Integer Dim t As Integer For i = 1 To 9For j = i + 1 To 10If a(i) a(j) Thent = a(i)a(i) = a(j)a(j) = tEnd IfNext jNext iFor i = 1 To 10Text2.Text = Text2.Text & a(i) & “ “Next iEnd Sub8 题目:请按照题目要求设计指定的窗体。-随机产生 10 个两位整数并将 10 个数采用冒泡排序的算法实现升序排列Option ExplicitDim a(
10、1 To 10) As Integer Private Sub Command1_Click()Dim i As Integer For i = 1 To 10a(i) = Int(Rnd * 90 + 10)Text1.Text = Text1.Text & a(i) & “ “Next iEnd SubPrivate Sub Command2_Click()Dim i As Integer 用来表示外围循环变量Dim j As Integer 用来表示内循环变量Dim t As Integer 用来表示临时变量For i = 1 To 9For j = 1 To 10 - iIf a(j)
11、 a(j + 1) Thent = a(j)a(j) = a(j + 1)a(j + 1) = tEnd IfNext jNext iFor i = 1 To 10Text2.Text = Text2.Text & a(i) & “ “Next iEnd Sub9.题目:请按照题目要求设计指定的窗体。-编程计算公式 S=1!+3!+5!+(2N-1)!的值。窗体设计界面参见如下。(1)要求 N 的值在文本框 Text1 中输入,累加和显示在 Text2 中;(2)程序中应定义一个通用函数过程 fact(),用以计算某个整数的阶乘Option ExplicitPrivate Sub Comman
12、d1_Click()Dim i As Integer 表示循环变量Dim n As Integer 表示计算的项数Dim s As Integer 表示最后的和n = Val(Text1.Text)For i = 1 To ns = s + fact(2 * i - 1)Next iText2.Text = sEnd SubPrivate Function fact(x As Integer) As IntegerDim i As Integer 用于表示循环变量fact = 1For i = 1 To xfact = fact * iNext iEnd Function10 题目:请按照题目
13、要求设计指定的窗体。-编写程序,找出 100 以内的所有素数,存放在数组 P 中, 并将所找到的素数,按每行10 个的形式显示在文本框 Text1 中。程序界面如下图所示:(1)按“找素数”按钮,则查找素数,并按格式要求显示在多行文本框中;(2)程序中应定义一个通用函数过程 Prime(),以判断一个数是否为素数;Option ExplicitOption Base 1Private Sub Command1_Click()Dim i As Integer, n As Integer, P() As IntegerFor i = 2 To 100If prime(i) Thenn = n +
14、1ReDim Preserve P(n)P(n) = iEnd IfNext iFor i = 1 To UBound(P)Text1 = Text1 & Right(“ “ & P(i), 3)If i Mod 10 = 0 Then Text1 = Text1 & vbCrLfNext iEnd SubPrivate Function prime(n As Integer) As BooleanDim i As IntegerFor i = 2 To Sqr(n)If n Mod i = 0 Then Exit FunctionNext iprime = TrueEnd FunctionPrivate Sub Command2_Click()Text1 = “End SubPrivate Sub Command3_Click()EndEnd Sub