计算机VB第二章课后参考答案
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
第二章VB程序入门
参考答案:
一、简答题(略)
二、找出以下语句中的错误
1.False --- 错,因为False是VB的关键字或保留字
2.Integers ----- 错,正确的写法是dim x ,y ,z as integer
3.a+b=c 改为c=a+b
4.Mode ---- 错,应改为mod
5.37001 --- 错,因为x取值范围是–32768 ---- 32767
6.÷----- 错,应该写为/
7..E ---- 错,E的左边必须是有一个数字
8.错,赋值符号的左边字符串缺了双引号
9. 1 --- 错…如是数字1,使用+将会出现”类型不匹配”的错误.如一定要使一字符串与一数字相联,可使用”&”运算符.或用str()函数将数字转换为字符串类型.10.错,可改成如下形式:#01-01-2000#
三、编写语句完成以下任务
1.设这3个数用x、y、z表示,则求3个数乘积的表达式为:x*y*z
如:Dim x as integer,y as integer , z as integer, mul as integer
Mul= x*y*z
2.Option explic it
3.Form1.print “Print Text to The Windows”
4.Dim result as long
Dim x as integer,y as integer , z as integer
Result=(x+y+z)/3
5.Dim x as integer, y as integer, z as integer
6.Y=3*x^5+2*x^4-6*x^3-1
四、数据常量有:”Xname”(字符串型)、True(逻辑型)、517.3(实数型) 、
"2/08/05"(日期时间型)、#1-1#(日期时间型)、
”String”(字符串型)、1(整型)
数据变量有:Xname、Step、X
五、写出下列表达式的值:
1. 6 注: 可用语句Print 2 * 3 ^ 3 mod 4*2 验证, 注意运算符的优先级.
2.2002-1-1 注: 日期数据可进行加减运算
3.xyz123abc
4.xyz123abc
5.12
6.11 注:可用语句x=10 : y=7 : Print y/2*x\3 验证, 优先级相同从左至右进行.
思考为什么会是这个得数.
5 7
-2 -9
False False
程序段2:
Visual
The Demo Basic
实验部分
1. '计算通过inputbox()接受三个数,在窗体上输出'它们的和,乘及平均值.
参考代码:
方法之一:
Private Sub Command1_Click()
Dim a As Single, b As Single,c As Single
a = InputBox("请输入第一个数a:")
b = InputBox("请输入第二个数b:")
c = InputBox("请输入第三个数c:")
Print "a="; a, "b="; b, "c="; c
Print "a + b + c = "; a + b + c
Print "a * b *c ="; a * b * c
Print " a + b + c / 3="; (a + b + c) / 3
End Sub
方法之二:
Private Sub Form_Click()
Dim x As Integer, y As Integer, z As Integer
x = InputBox("请输入:")
y = InputBox("请输入:")
z = InputBox("请输入:")
Print x + y + z
Print x * y * z
Print (x + y + z) / 3
End Sub
2.参考代码和界面如下:
Private Sub Command1_Click()
Dim r As Integer
r = InputBox("请输入圆半径:")
Label1.Caption = 2 * r & " "
Label1.Caption = Label1.Caption & 3.14 * r ^ 2 & " "
Label1.Caption = Label1.Caption & 2 * 3.14 * r
End Sub
r = InputBox("请输入圆半径:")
Label1.Caption = "直径:" & 2 * r
Label1.Caption = Label1.Caption & " 周长:" & 3.14 * r ^ 2
Label1.Caption = Label1.Caption & " 面积:" & 2 * 3.14 * r
End Sub
Private Sub Command3_Click()
End
End Sub
3.参考代码:
Private Sub Form_Click()
Dim a As Integer
a = InputBox("请输入16或33或53...:") …输入的值分别是16,33…..等MsgBox "注意观测!", a, "消息框"
End Sub
思考: a 代表MsgBox中的什么参数?
4.参考代码和界面如下:
方法之一:
Private Sub command1_Click()
Dim temp As Integer
text1.Text = ""
text2.Text = ""
text3.Text = ""
Label4.Caption = "+"
text1.Text = InputBox("请输入第一个数:")
text2.Text = InputBox("请输入第二个数:")
temp = V al(text1.Text) + V al(text2.Text)
text3.Text = temp
End Sub
Private Sub command2_Click()
Dim temp As Integer
text1.Text = ""
text2.Text = ""
text3.Text = ""
Label4.Caption = "-"
text1.Text = InputBox("请输入第一个数:")
text2.Text = InputBox("请输入第二个数:")
temp = V al(text1.Text) - V al(text2.Text)
text3.Text = temp
End Sub
Private Sub Command3_Click()
Dim temp As Integer
text1.Text = ""
text1.Text = InputBox("请输入第一个数:") text2.Text = InputBox("请输入第二个数:") temp = V al(text1.Text) * V al(text2.Text)
text3.Text = temp
End Sub
Private Sub Command4_Click()
Dim temp As Single
text1.Text = ""
text2.Text = ""
text3.Text = ""
Label4.Caption = "/"
text1.Text = InputBox("请输入第一个数:") text2.Text = InputBox("请输入第二个数:") temp = V al(text1.Text) / V al(text2.Text)
text3.Text = temp
End Sub
方法之二:
Private Sub command1_Click()
Dim temp As Integer
text1.Text = ""
Text2.Text = ""
text3.Text = ""
Label4.Caption = Command1.Caption
text1.Text = InputBox("请输入第一个数:") Text2.Text = InputBox("请输入第二个数:") temp = V al(text1.Text) + V al(Text2.Text)
text3.Text = temp
End Sub
Private Sub command2_Click()
Dim temp As Integer
text1.Text = ""
Text2.Text = ""
text3.Text = ""
Label4.Caption = Command2.Caption
text1.Text = InputBox("请输入第一个数:") Text2.Text = InputBox("请输入第二个数:") temp = V al(text1.Text) - V al(Text2.Text)
text3.Text = temp
End Sub
Private Sub Command3_Click()
Dim temp As Integer
text1.Text = ""
Label4.Caption = Command3.Caption
text1.Text = InputBox("请输入第一个数:") Text2.Text = InputBox("请输入第二个数:") temp = V al(text1.Text) * V al(Text2.Text)
text3.Text = temp
End Sub
Private Sub Command4_Click()
Dim temp As Single
text1.Text = ""
Text2.Text = ""
text3.Text = ""
Label4.Caption = Command4.Caption
text1.Text = InputBox("请输入第一个数:") Text2.Text = InputBox("请输入第二个数:") temp = V al(text1.Text) / V al(Text2.Text)
text3.Text = temp
End Sub。