实验四 循环结构程序设计

更新时间:2023-12-14 13:00:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

实验四 循环结构程序设计(4个学时) 一 、实验目的

1. 掌握For循环语句的使用。 2. 掌握单Do循环语句的使用。

3. 掌握如何控制循环条件,防止死循环或不循环。

二 、实验内容

1. 设计一个程序:用For 循环语句计算1~ n (正整数)的所有的偶数之和。(参考课本P98,例6.7)

要求: 程序运行后,单击窗体弹出一个输入框由用户输入n的值,然后在窗体上显示计算的结果。

1)代码设计如下

Private Sub Form_Click()

Dim a As Single, i As Single, n As Single n = Val(InputBox(\请输入n\提示\s = 0

For i = 2 To n Step 2 s = s + i Next i Print s

End Sub

2)将输入100 时的运行结果的界面截下来粘贴在下面:

3)调试过程记录(错误情况及改正方法) 1.在做FOR循环时,将变量I漏写。

2.设计程序:求100 以内所有19的倍数的数。并求这些数的和。(参考上机指导P20,第一题)

要求:点击“显示”按钮时,将结果显示在窗体上。 点击“求和”按钮时,将结果显示在一个文本框中。

1)代码设计如下 Option Explicit

Dim i As Integer, s As Integer, t As String

Private Sub Command1_Click() For i = 1 To 100

If i Mod 19 = 0 Then Print i Next i

End Sub

Private Sub Command2_Click() For i = 1 To 100 If i Mod 19 = 0 Then

s = s + i End If Next i

Text1.Text = s

End Sub

2)将运行结果的界面截下来粘贴在下面

3)调试过程记录(错误情况及改正方法)

1.在对命令按钮2进行设计时以为只写命令1的就行,结果命令2 没有FOR结构。 2.误将零写成o,结果无意中增加了一个未定义的新变量。

3. 设计一个程序:我国今年的国民生产总值为45600亿元,若今后每年以9%的增长率增长,计算多少年后能实现国民生产总值翻一番? (参考课本P102 例6.11)

要求:单击“计算”按钮时,将计算的结果显示在窗体上。

1)代码设计

Private Sub Command1_Click() Dim x As Single, n As Long x = 45600

Do While x < 91200

x = x * 1.09 n = n + 1 Loop

Print n; \年后国民生产总值翻一番\End Sub

2)将运行结果的界面截下来粘贴在下面

3)调试过程记录(错误情况及改正方法)

1.编码时将X的初值放在了DO句型的后面,结果出现了死循环。

4. 编写一个程序,点击窗体时在窗体上输出所有水仙花数(水仙花数是指一个三位十进制正整数,该数的各位数字的立方之和等于该数本身,例如153是个水仙花数,因为1^3+5^3+3^3 =153)。

1)代码设计

Private Sub Command1_Click() Dim i!, a!, c!, d!, t As String For i = 100 To 999 Step 1 a = i \\ 100

c = (i Mod 100) \\ 10 d = i Mod 10

If i = a ^ 3 + c ^ 3 + d ^ 3 Then t = t & Str(i) & Chr(13) & Chr(10) End If Next i

Text1.Text = t End Sub

2)将程序运行的结果图截下粘贴在下面。

3)调试过程记录(错误情况及改正方法) Textbox换行没设置,在multiline处设置

5. 编写一个程序解百马百瓦古题:大、小马和马驹共100匹,共驮100片瓦,大马一驮三,小马一驮二,马驹二驮一,一次驮完,三种马都驮,共有多少种组合?(参考课本P106,例6.16)。

要求:单击窗体将结果显示在窗体上。

1)代码设计

Private Sub Form_Load() Dim x%, y%, z% Show

Print \大马\小马\马驹\For x = 1 To 33 For y = 1 To 50 z = 100 - x - y

If x * 3 + y * 2 + z / 2 = 100 Then Print x, y, z End If Next y Next x

End Sub

2)将程序运行的结果图截下粘贴在下面。

3)调试过程记录(错误情况及改正方法) Next y 和next x的顺序打反,导致无法运行。

实验五 常用控件程序设计(4个学时) 一 、实验目的

1. 掌握单选按钮、复选按钮、框架、列表框、组合框、时钟和滚动条的属性、事件和方法。 2. 明确常用内部控件的功能,熟练使用“属性”窗口设置属性。 3. 掌握在常用内部控件的事件过程中编写程序代码。

二 、实验内容

1. 设计一个程序运用单选按钮、复选框和框架对文本框中文本的颜色、字号和效果进行设置。

要求:文本的颜色可以选择红色、绿色和蓝色。文本的字号可以选择12、18、24号,效果可以选择删除线和下划线。当文本框中输入文字后,单击某个按钮,文本框中的文字将按所选择的选项进行设置。(参考上机指导P6,第一题)

1)代码设计如下

2)运行后的结果 (将运行结果的界面截下来粘贴在下面)

3)调试过程记录(错误情况及改正方法,不得少于三条)

2. 编写一个为组合框中添加或删除内容的程序。(参考上机指导P6第三题) 要求:

1)在组合框中输入内容后,单击“添加”按钮,如果列表框中没有该内容,则将输入内容加入到列表框中,否则不添加,另外,要求组合框中的内容能自动按字母顺序排序。 2)在列表框中选择某一选项后,单击“删除”按钮,则删除该项。

3)单击“清除’按钮,将清除列表框中所有的内容。

1)代码设计

2)运行后的结果 (将运行结果的界面截下来粘贴在下面)

3)调试过程记录(错误情况及改正方法,不得少于三条)

3.设计一个选课程序。

要求:该选课的运行界面包含2个列表框,左边为已开设的课程名称,通过Form_load事件加载,并按字母顺序排序;当单击某课程名称后,将该课程加入到右边的列表框中,并在左边的列表框中删除该课程,当右边的课程数已满5门时,不允许再加入。

1) 代码设计

2) 运行后的结果 (将运行结果的界面截下来粘贴在下面)

3) 调试过程记录(错误情况及改正方法,不得少于三条)

4. 设计一个程序实现字体的自动放大,并用垂直滚动条控制放大的速度。(参考课本P86,例5.7)

1)代码设计

2)运行后的结果 (将运行结果的界面截下来粘贴在下面)

3)调试过程记录(错误情况及改正方法,不得少于三条)

A:Hello,everyone, I am very famous . My name is QQ, people call me 阿-Q. I am very famous , I love my job very much . You know I am a worker .But I hate my boss .He says he is a foreigner, but I don’t believe that. (听见老板的咳嗽声)Hey master!

B:(咳嗽) A-Q,you know tomorrow is my father’s birthday.

A:Oh,your father’s birthday?How old is your father ? B:He is dead.

A:Oh,he……I am sorry to hear that…… But when did your father die? B:Two years before I was born.

A:Oh,two years before you were born ?

B:So, tomorrow, 阿Q,you should work harder.

A:Of course ,of course I will work harder.额……

But master ……额……I just want some

more money……(some more money) B:You want some more money? A:Yes,some more money master.

B:(一巴掌扇过来)Tell you , no way! A:NO way?

B:But I can find somebody to help you A:Somebody to help me?who?

B:WU MA,come here,WU MA he quick! A:wu ma

B:AQ,this is WUMA,wuma,now you just work together with AQ.

A:一直在转着圈得说着 无码 B:AQ…… A:Master

B:This is wuma A :I know

B:YOU know from now you just work together with her. A NO problem

B:Do remember ,do not do anything wrong to her, or I will kill you A:Of course,not

B:It’s ten o’clock. I just want to go to buy some animals to eat

A:Oh animals to eat ,ok . ok……

B: Remember , I will be back, do not do anything wrong to her.

A Of course not ,take it easy.

H: wuma wuma wuma wuma what’s your name ?wuma

W.My name is wuma

A:wuma ,how many husbands do you have? W.Only one

A :She says only one,how old is he ? W He is dead.

A :He is dead! I am very glad to hear that. Wuma wuma

You know,my name is qq,people call me A-Q wuma you know, I have a large house, I make money everyday, a lot of money. W:So you have a lot of wives?

A: A lot of wives? Oh, no , I don't have any wives

But I have two dogs and three cats and four

monkeys.

W: Are they beautiful?

A: Of course they are very beautiful. But you are more beautiful than animals.

Im sorry I mean animals are more beautiful than you im sorry I mean you are as beautiful as animals. A. wuma… to be frank , to tell you the truth , actually in my heart in fact……I just want to marry you ,wuma I just want you to be my wife.wuma,I just want to sleep with you. W:What did you say

A:Wuma I just want to sleep with you

W:wolf wolf ,no no help help, policeman policeman… A:wuma,

P: Where is the wolf W:it’s here.

P: It’s A-Q,he is not a wolf , don't care about it Beautiful woman!

May I know your name ?

W.My name is wuma,people call me wuma

P:Oh, wuma,how many husband do you have? W. only one

P : Oh, only one. How old is he? W:He is dead P:Dead ? good

P :wuma I just want you to be my wife NO.6 to be my wife NO.6

wuma, I am a good person .i’m very very good,

you see I’m good P:Shit,I will kill you ! A.You can’t hurt her P:Look , beautiful girl!

A: I’m hurt, I am dying wuma

Wuma wuma you know I am not a wolf W.:I know , I know.

A:wuma you know , I love you very much. Wuma , I just want you to be my wife, ok? W:ok, I will be your wife.

A:But wuma you know I am very poor. W: I don't care, I don’t care A :oh, wuma, I love you.

W: A-Q, I love you too.

本文来源:https://www.bwwdw.com/article/oj15.html

Top