第一篇:VB上机考试 习题以及答案
VB上机考试
1、利用随机函数产生25个100到999之间的整数,构成55的矩阵,输出该矩阵。Dim a(1 To 5, 1 To 5)Private Sub Form_Click()Randomize For i = 1 To 5 For j = 1 To 5 a(i, j)= Int(Rnd *(99910 + 1)+ 10)Print a;s = s + a Next i Print Avg = s / 25 Print Avg End Sub
3、在窗体上按下鼠标的右键,记录其坐标。
Private Sub Form_Load()Label1.BorderStyle = 1 Label1.Alignment = 2 End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)If Button = 2 Then Label1.Caption = X & “,” & Y End If End Sub
4、利用随机函数产生25个10到99之间的整数,按从小到大的顺序显示。Dim a(1 To 25)Private Sub Form_Click()Cls For i = 1 To 25 a(i)= Int(Rnd *(991 For j = 1 To 251 + 1)+ 1)a(x)= a(x)+ 1 Next j For i = 1 To 6 Print i & “出现了” & a(i);“次” Next i
End Sub
6、在文本框中输入一些英语单词,统计空格的个数。
Dim n%, x As String, s% Private Sub Form_Click()n = Len(Text1.Text)x = Text1.Text For i = 1 To n If Mid(x, i, 1)= “ ” Then s = s + 1 Next i Print “共出现了” & s & “个空格” End Sub
7、编程计算下列公式的值,其中n(<10)为从键盘输入的正整数。S=1!+3!+5!+……+(2*n-1)!Private Sub Form_Click()n = Val(InputBox(“请输入一个不大于10的整数”))s = 0 For i = 1 To 2 * n2 * i1 a = a + 1 Print Str(a);Next j Print Next i End Sub
10、用For …Next语句编程,双击窗体后,在屏幕上显示如图:
Private Sub Form_DblClick()For i = 1 To 3 Print Tab(113 * i);For j = 1 To 5 Print x;Next j Print Next i End Sub
12、从文本框输入一个正整数,判断该数是否为素数。Dim a As Long Private Sub Form_Click()a = Val(Text1.Text)If a = 2 Then Tag = True ElseIf a Mod 2 = 0 Then Tag = False Else Tag = True For i = 3 To Sqr(a)Step 2 If a Mod i = 0 Then Tag = False: Exit For Next i End If If Tag = True Then Print a;“是素数” Else Print a;“不是素数” End If
End Sub
13、利用随机函数产生并显示10个3位数的正整数,找出其中的最大数。Option Explicit Dim i%, max%, loc% Dim a(1 To 10)As Integer Private Sub Form_Click()Randomize Form1.Cls For i = 1 To 10 a(i)= Int(Rnd *(9991000 + 1)+ 1000)Print a(i)Next i min = 9999 For i = 1 To 10 If a(i)< min Then min = a(i): loc = i Next i Print “最小的数是”;min Print “在数组中是第”;loc;“个数” End Sub
15、当按下鼠标左键时,窗体的背景色变为红色;松开鼠标左键时,窗体的背景色变为绿色。Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)If Button = 1 Then Form1.BackColor = vbRed
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)If Button = 1 Then Form1.BackColor = vbGreen End Sub
16、编制一个时钟程序,如下图
:Option Explicit
Private Sub Form_Load()Label1.BackColor = vbWhite Label1.BorderStyle = 1 Label1.Alignment = 2 Timer1.Enabled = True Timer1.Interval = 1000 End Sub
Private Sub Timer1_Timer()Label1.Caption = Time End Sub
17、从键盘输入10个数,按输入的顺序和按输入顺序相反的顺序输出。Option Explicit Dim i% Dim a(1 To 10)As Single Private Sub Form_Click()For i = 1 To 10 a(i)= Val(InputBox(“请输入第” & i & “个数”))Print a(i);Next i Print For i = 10 To 1 Step-1 Print a(i);Next i
End Sub
18、计算圆面积的程序。程序运行时,单击窗体,然后在下图所示的输入框中输入圆的半径,即可在窗体上显示计算出的圆面积。
Option Explicit Dim r%, a As Single Const pi As Single = 3.14159 Private Sub Form_Click()r = Val(InputBox(“请输入圆半径” & vbCrLf & “(1-9):”, “输入”, 5))a = pi * r ^ 2 Print “半径为”;r;“的圆面积为”;a End Sub
19、编写一个时钟程序,当选择“24小时”时,以24小时的方式显示如A,当选择“12小时”时,以12小时的方式显示如B。
Private Sub Form_Load()Timer1.Interval = 1000 End Sub
Private Sub Timer1_Timer()Select Case True Case Option1.Value Label1.Caption = Time Case Option2.Value Label1.Caption = Format(Time, “HH:MM:SS AM/PM”)End Select End Sub 20、设计一个程序,用户界面如图所示。程序运行后,用户在文本框中输入一段文字,然后按需要单击选择框,用以改变文本的字体、字型、颜色以及大小。
Option Explicit
Private Sub Check1_Click()If Check1.Value = 1 Then Text1.FontName = “黑体” Else Text1.FontName = “宋体” End If End Sub
Private Sub Check2_Click()If Check2.Value = 1 Then Text1.FontUnderline = True Else Text1.FontUnderline = False End If End Sub
Private Sub Check3_Click()If Check3.Value = 1 Then Text1.FontSize = 20 Else
Text1.FontSize = 9 End If End Sub
Private Sub Check4_Click()If Check4.Value = 1 Then Text1.ForeColor = vbGreen Else Text1.ForeColor = vbBlack End If End Sub
Private Sub Command1_Click()Text1.Text = “" End Sub
Private Sub Command2_Click()End End Sub 20做出2个周期的正弦波形.Private Sub Form_Click()Dim i%, x#, y# Const pi As String = 3.1415926 Form1.Scale(-2 * pi, 1.2)-(2 * pi,-1.2)Form1.Line(-2 * pi, 0)-(2 * pi, 0)Form1.Line(0, 1.2)-(0,-1.2)For x =-2 * pi To 2 * pi Step 0.01
y = Sin(x)
Form1.PSet(x, y)Next x End Sub 21以窗体为中心,产生1000个半径颜色随机的同心圆.Private Sub Form_Click()For i = 1 To 1000
x = Form1.ScaleWidth / 2
y = Form1.ScaleHeight / 2
r = Int(Rnd * y)
c = RGB(255 * Rnd, 255 * Rnd, 255 * Rnd)
Form1.Circle(x, y), r, c Next End Sub 22.九九乘法表 Private Sub Form_Click()Dim i%, j% Form1.Caption = ”九九乘法表“ Print Tab(48);Caption Print Tab(48);”-------------“ For i = 1 To 9
For j = 1 To i
Print Tab((j-1)* 12 + 1);i;”*“;j;”=“;i * j;
Next j Print Next i End Sub 23求最大公约数和最小公倍数 Private Sub Command2_Click()Text1.Text = ”“ End Sub
Private Sub command1_Click()Dim m%, n%, r%, t% m = Val(Text1.Text)n = Val(Text2.Text)If m < n Then t = m: m = n: n = t t = m * n Do
r = m Mod n
m = n
n = r Loop Until r = 0 Text3.Caption = ”公约数:“ & m Text4.Caption = ”公倍数" & t / m End Sub 24.用随机函数产生50个两位数,保存于一个一维数组中,要求找出其中的最大值,最小值及所在位置.
第二篇:2011级VB上机实习题
一、用记事本新建一个文本文件,将本班的M名学生在本学期的N门考试课的成绩(要求用假想分数,并要求将自己的每门课的分数定在96分以上,放在第一列;而将别人的大多数课程的分数都定在96分以下)输入到这个文件中,建议按如下形式存放(注:20≤M≤50、5≤N≤8,下文以M=4,N=3为例):
学号姓名英语VB数学
1我自已969899
2李某919667
3王某755856
4赵某586777
二、将本学期的N门课程及其学分(16学时为1个学分)输入到另一个数据文件(文件类型同上题)中,建议如下列形式存放:
编号课程名称课程学分
1英语5.0
2vb程序设计4
3数学4.5三、请编写一个简单的学生成绩管理程序,要求完成以下功能:
1)根据以上两文件内容分别计算每个学生N门课的加权平均分(保留2位小数,4舍5入),按平均
分排列名次,平均分相同则名次并列,如第1、2、2、4、5、5、7…名。用VB创建一个数据文件,存放班级的成绩表,建议按以下形式存放。
学号姓名英语C程序设计数学平均成绩名次
1张某76898980.52
2李某91966778.23
3王某75585688.81
4赵某58677768.54
2)分别统计全班每门课的平均分,并计算各分数段(60分以下;60~69;70~79;80~89;90
分以上)的学生人数。用VB创建一个新的数据文件,在该文件中存放数据的格式如下: 范围.英语VB......>=9035.......80-891012....................................3)打印每名学生成绩条(并将结果存放到用VB创建的一个新的数据文件中)。
4)打印所有存在不及格课程学生的名单(含学号、不及格课程名称、课程的学分及成绩)。
5)打印优等生名单(学号、N门课成绩、平均分名次)
优等生条件:
a)平均分 90分
b)名次在本班位于前三名
c)平均分85分,并且有二门课的成绩都 95分
选做内容:打印每门课成绩分布图
说明:所有输出结果应写入用VB创建的文件中,格式要整齐,并有适当的文字说明。实习报告要求:应包括分析设计与说明、部分过程(函数)的源程序清单、程序调试过程中遇到的问题及解决方法,有待改善或改进的意见,对本次实习的意见与建议、收获等。其中原始数据及程序运行的结果应作为附录附于实习报告之后。
第三篇:VB上机试题及答案
上机操作练习题
1.某数组有20个元素,其值为随机产生的两位整数。现要求将前10个元素与后10个元素对换。即第1个元素与第20个元素互换,第2个数与第19个元素互换,…,第10个元素与第11个元素互换。在窗体上输出数组原来各元素的值与对换后各元素的值。
Option explicit Dim a(1 to 20)as integer, I as integer, j as integer, t as integer Private Sub Form_Load()Show Print “原始数组是:” Randomize For i = 1 To 20
a(i)= Int(Rnd * 90)+ 10
Print a(i);If i Mod 10 = 0 Then Print Next i Print Print “现在数组是:” For i = 1 To 10
j = 211
a(k)= a(k + 1)
Next k
ReDim Preserve a(UBound(a)1 End If i = i + 1 Loop While(i <= UBound(a))If flag = False Then MsgBox n & “不在该数组中” Else Print “删除” & n & “后的数组是:” For i = 0 To UBound(a)Print a(i);Next i End If End Sub 3.数组a中存放着升序排列的数据,使用array赋值,将inputbox输入的新数插入到数组中,插入后数组仍有序。
注意:要求利用动态数组,能够实现多次插入 Private Sub Form_Load()Show n = 8: ReDim a(n)a = Array(5, 15, 23, 52, 68, 88, 90, 99)n = n + 1: ReDim Preserve a(n)m = InputBox(“请输入要插入的数据:”)For i = 1 To n1)Next k a(k)= m For i = 1 To n
Print a(i);Next i Print End Sub 4.6个评委为4位歌手打分,分数介于1-100之间。去掉一个最高分,一个最低分,求出每位选手的最后得分。将4位歌手得分从高到低排列输出,同时输出该选手的编号。
如:
歌手3 95分 歌手 1 87分 歌手 4 80分 歌手 2 70分
/ 19
Option Base 1
Private Type geshou
xuhao As String
fenshu(6)As Integer
score As Integer
pingjun As Integer End Type Dim singer(1 To 4)As geshou Private Sub Command1_Click()For i = 1 To 4 For j = 1 To 6
singer(i).fenshu(j)= Int(Rnd * 100)
singer(i).xuhao = “歌手” & i Next Next Call paixu End Sub
Private Sub paixu()Dim p As geshou For i = 1 To 4
For j = 1 To 5
For k = j To 6
If singer(i).fenshu(j)> singer(i).fenshu(k)Then
t = singer(i).fenshu(j)
singer(i).fenshu(j)= singer(i).fenshu(k)
singer(i).fenshu(k)= t
End If
Next
Next Next For i = 1 To 4 For j = 2 To 5
singer(i).score = singer(i).score + singer(i).fenshu(j)Next singer(i).pingjun = singer(i).score / 4 Next
For i = 1 To 3 For j = i To 4
If singer(i).score < singer(j).score Then
p = singer(i): singer(i)= singer(j): singer(j)= p
End If Next
/ 19
Next For i = 1 To 4 Print singer(i).xuhao & “得分:” & singer(i).pingjun Next End Sub Private Sub Form_Load()End Sub 10个评委给10个选手打分 Private Sub Form_Load()Show n = UBound(b)For i = 1 To 10
For j = 1 To 10
a(i, j)= Val(InputBox(“请第” & j & “位评委给第” & i & “位选手打分:”))Next j Next i For i = 1 To 10
max = a(i, 1): sum = 0
min = a(i, 1): ave = 0
For j = 1 To 10
If a(i, j)> max Then max = a(i, j)
If a(i, j)< min Then min = a(i, j)
sum = sum + a(i, j)
Next j
ave =(summin)/(n1
For j = i + 1 To n
If b(i)< b(j)Then
t = b(i): b(i)= b(j): b(j)= t
End If
Next j Next i Print “选手的得分由高到低为:” For i = 1 To 10
Print b(i);Next i End Sub 5.编写函数求一元二次方程ax2+bx+c=0的解。
注意:要求考虑方程有两相等实根、两不等实根和两个虚根的情况。Dim a As Single, b As Single, c As Single Dim s As Single a = InputBox(“请输入a”)
/ 19
b = InputBox(“请输入b”)c = InputBox(“请输入c”)s = b ^ 2Sqr(s))/ 2 * a End If End Sub 6.任意输入一组字符,统计出大写字母多少个,小写字母多少个,数字多少个?要求用过程实现。
Call tongji(a, n, m, o)Print “数字:” & n & Chr(13)& “小写:” & m & Chr(13)& “大写:” & o End Sub Private Sub tongji(a As String, n As Integer, m As Integer, p As Integer)For i = 1 To Len(a)If Asc(Mid(a, i, 1))> 47 And Asc(Mid(a, i, 1))< 58 Then
n = n + 1 ElseIf Asc(Mid(a, i, 1))> 96 And Asc(Mid(a, i, 1))< 123 Then
m = m + 1 ElseIf Asc(Mid(a, i, 1))> 64 And Asc(Mid(a, i, 1))< 91 Then
p = p + 1 End If Next End Sub 7.随机产生一个5*5的矩阵,将主对角线和次对角线元素都置为1,并求出矩阵的四周元素之和,显示在列表框中。
Option Base 1 Private Sub Form_Load()Dim a(5, 5)As Integer Dim s As Integer For i = 1 To 5 For j = 1 To 5
If i = j Or i = 6Asc(Mid(s1, i, 1))+ 64)ElseIf Asc(Mid(s1, i, 1))< 123 And Asc(Mid(s1, i, 1))> 96 Then convert = convert & Chr(1235: g = e
For j = 1 To n
If j > i Then
a(i, j)= 0
Else
If i = j Then
a(i, j)= 1
Else
a(i, j)= a(i1, ji + 1);String(2 *(i1)/ 2 To 1 Step(-1)
c = c + 1
s = Chr(64 + ii + c + 1);String(2 *(j1)/ 2 Else
x = x / 2 End If i = i + 1 Loop
ReDim Preserve a(UBound(a))If y < 0 Then For i = 1 To UBound(a)convert = convert & a(i)Next convert = “-” & convert Else For i = 1 To UBound(a)
/ 19
convert = convert & a(i)Next End If
End Function Private Sub Command1_Click()Dim y As Integer Text1.Text = “" y = InputBox(”请输入十进制数字“)Text1.Text = convert(y)
End Sub
Private Sub Form_Load()
End Sub 14.输入一个整数N,产生N个随机数(范围1—10*N),然后采用任何2种算法实现由小到大的排序。将排序前后的数据显示出来。
Option Base 1 Private Sub Command1_Click()Dim a()As Integer, n As Integer n = InputBox(”请输入数列项数“)ReDim a(n)For i = 1 To n
a(i)= Int(Rnd * 10 * n + 1)
Print a(i);Next Print ReDim Preserve a(n)For i = 1 To n1 For j = 1 To n2)+ fact(n2 s = s1 + s2: t = s2: s2 = s: s1 = t Next End Function
Private Sub Command2_Click()n = InputBox(”请输入n“)Print s(n)End Sub 17.编写2个函数,分别采用递归法和递推法求斐波纳契数列的第n个月的兔子数,n使用inputbox输入,调用函数的结果在窗体上显示。Private Sub Command1_Click()List1.Clear Dim i As Integer For i = Val(Text1.Text)To Val(Text2.Text)If isprime(i)= True Then
List1.AddItem(i)
c = c + 1
If i Mod 10 = 9 Then
/ 19
List1.Selected(c1 If n Mod i = 0 Then
isprime = False
Exit For End If Next End Function 18 Dim sum As Long Private Sub Command1_Click()
Dim i As Integer, j As Integer, a(40)As Long
a(1)= 1
a(2)= 1
For i = 3 To 40
a(i)= a(i2)
Next i
sum = 0
For j = 1 To 40
sum = sum + a(j)
Next j End Sub
Private Sub Command2_Click()
Open ”out18.txt“ For Output As #1
Print #1, ”数列1,1,2,3,5,8…的前四十项的之和是:“
Print #1, sum
Close #1 End Sub 1.百钱买百鸡
Option Explicit
Private Sub Command1_Click()Dim a As Integer, b As Integer For a = 1 To 34
For b = 1 To 20
If 3 * a + 5 * b +(100b)/ 3 = 100 Then
/ 19
Print a;
Print b;
Print 100b
End If
Next b Next a End Sub 2.分钱作业
Private Sub Command1_Click()Dim a As Single,b as single,c as single a = Text1.Text b = Fix(a / 100)Print ”100元:“;Print b c = ab * 50
b = Fix(c / 20)Print ”20元:“;Print b c = c0.05 * b b = Fix((c + 0.005)/ 0.02)Print ”0.02元:“;Print b c = c1
If a(i)= a(j)Then
GoTo aaa
End If
Next j
Next i For i = 1 To 10
Print a(i);Next i Print End Sub
Private Sub Command2_Click()Dim i As Integer, j As Integer, t As Integer For i = 1 To 9
For j = 1 + i To 10
If a(i)> a(j)Then
t = a(j)
a(j)= a(i)
a(i)= t
End If
Next j Next i For i = 1 To 10
Print a(i);Next i Print
End Sub
Private Sub Command3_Click()Dim i As Integer, j As Integer, t As Integer For i = 1 To 9
For j = 1 To 10c)> 10 ^(-5)i =(a + b)/ 2
If i ^ 2c = 0 Then
Exit Do
Else
a = i
End If Loop MsgBox(i)End Sub 7.闰年
Option Explicit
Private Sub 计算_Click()Dim i As Integer i = InputBox(”请输入一个年份“)If i Mod 4 = 0 And i Mod 400 = 0 Then
If i Mod 100 <> 0 Then
MsgBox ”NOT“
Else
MsgBox ”YES“
End If Else
MsgBox ”NOT“ End If
End Sub 8.三角
Option Explicit Private Sub Command1_Click()
/ 19
Dim a As Single Dim b As Single Dim c As Single Dim p As Single Dim s As Single
a = Text1.Text b = Text2.Text c = Text3.Text
p =(a + b + c)/ 2
s = Sqr((pb)* p +(p1
If n Mod i = 0 Then
MsgBox ”NOT“
Exit For
End If Next i If i = n Then
MsgBox ”YES“ End If End Sub
Private Sub Command2_Click()
Dim a As Boolean, n As Double, i As Double a = False n = InputBox(”请输入一个数:“)For i = 2 To n-1
If n Mod i = 0 Then
a = True
Exit For
End If Next i If a = True Then
/ 19
MsgBox ”NOT“ Else
MsgBox ”YES“ End If End Sub
Private Sub Command3_Click()Dim n As Double, i As Double n = InputBox(”请输入一个数:“)i = 2 Do While i < n
If n Mod i = 0 Then
MsgBox ”NOT“
Exit Do
End If i = i + 1 Loop If i = n Then
MsgBox ”YES“ End If End Sub
Private Sub Command4_Click()Dim n As Double, i As Double, sushu As Boolean n = InputBox(”请输入一个数:“)i = 2 sushu = True Do While i < n And sushu = True
If n Mod i = 0 Then
sushu = False
End If i = i + 1 Loop If sushu = True Then
MsgBox ”YES“ Else
MsgBox ”NOT" End If End Sub
/ 19
第四篇:VB上机试题及答案
1、在工程中添加一个窗体form1,当用户单击窗体时,在窗体上显示九九乘法表程序。结果如下图所示(本题编程解决,20分)
参考答案:
Private Sub Form_Click()
Cls
For i = 1 To 9
For j = 1 To i
Print i * j;“ ”;
Next j
Next i End Sub
2、在工程中,添加一个窗体form2,在窗体上放置一个文本框text1(文本框中的文字为“改变字形”,居中显示)。三个复选框check1,check2和check3,如图设置属性。
要求:当用户单击“加粗”、“斜体”、“下划线”这三个复选框时(即当复选框被选中时)能将文本框的字形变为“粗体、斜体、加下划线”(本题编程完成,满分:30分)
参考答案:
Private Sub Check1_Click()
Text1.FontBold = Check1.Value End Sub
Private Sub Check2_Click()
Text1.FontItalic = Check2.Value End Sub
Private Sub Check3_Click()
Text1.FontUnderline = Check3.Value End Sub
3、在工程中添加一窗体form3,在窗体上按下图放置两个标签和一个文本框,一个列表框和一个命令按钮,各控件的名称属性取默认值。其他属性按下图进行设置。
要求:当用户在文本框输入他的籍贯后,单击“添加籍贯”按钮时,能够将籍贯添加到列表框中。双击列表框中的某一项时将该项删除.(本题:20分)
参考答案:
Private Sub Command1_Click()
List1.AddItem Text1.Text End Sub
Private Sub List1_DblClick()
List1.RemoveItem List1.ListIndex End Sub
4、在窗体form4中建立下图所示的菜单,各菜单项的名称如下:
文件:file,新建:NEW,-:kong1,打开:OPEN,关闭:CLOSE 编辑:EDIT,剪切:CUT,复制:COPY,粘贴:PASTE(本题不用编程,30分)
参考答案:略
第五篇:vb上机心得体会
心得体会:
刚开始学习vb的时候,我就觉得大一的计算机基础根本不值一提,难度相去甚远。我问过很多以前的同学,他们都说vb是编程语言中最简单的,但是在这次上机实习过程中,我发现要完成学生成绩管理系统实在有很大难度。平时看课本的时候,觉得还能够理解,看得懂,然而“纸上得来终觉浅”,一旦自己调试程序,总会犯很多错误,如前面说的“文件未找到”“下标越界”等等,都是平时没有机会解决和一些曾经被自己忽视的问题。有时候,为了找到代码中错误或不符合要求的地方,我不得不一遍又一遍地仔细检查程序,甚至标点符号都不能放过。细节决定成败,一个小小的分号也会起着扭转乾坤的作用。
在实习之前,我妄想从网站上下载一个或者请论坛上的好友帮忙,后来我发现这条路走不通,他们从来都不给别人提供实质性的帮助,只是象征性地说:“不会问老师。”于是我打消了这个念头,开始自己动手做。
当我无数次调试总算读出文件的时候,那种高兴劲可想而知。我慢慢觉得,其实自己调试程序蛮有意思的。既弥补了在vb学习中的漏洞,还学到了新内容,积累了一定的实战经验,对以后的期末考试和等级考试起到了相当大的促进作用。
实习过程中,多亏指导老师帮助我找出错误的地方。如果不是他们的细心讲解,我很难完成这次实习任务。当然,我在编程方面还有很大不足,今后我定会加倍努力,为等级考试的顺利通过打下牢固的基础。