Presentation is loading. Please wait.

Presentation is loading. Please wait.

3.16 枚举算法及其程序实现 ——数组的作用.

Similar presentations


Presentation on theme: "3.16 枚举算法及其程序实现 ——数组的作用."— Presentation transcript:

1 3.16 枚举算法及其程序实现 ——数组的作用

2 用数组表示“石头剪刀布” Dim choice(1 To 3) As String choice(1) = "石头"
computer = Int(Rnd * 3) + 1 Print computer Command1.Caption = choice(computer)

3 数组的定义 Dim 数组变量名(a1 to a2) as 元素的类型 Dim result(3) as integer 表示定义了:
result(0)、result(1)、result(2)、result(3)

4 小试牛刀 用数组定义“石头剪刀布”的结果,然后直接输出。
Dim choice(1 To 3) As String, result(-2 To 2) As String Private Sub Form_Load() choice(1) = "石头" choice(2) = "剪刀" choice(3) = "布" result(-2) = "" result(-1) = "" result(0) = "" result(1) = "" result(2) = "" Randomize End Sub Private Sub Command1_Click() Command2.Visible = False '将command2设为不可见 Command3.Visible = False Dim computer As Integer computer = Int(Rnd * 3) + 1 Command6.Caption = choice(computer) Label3.Caption = 【】 End Sub

5 课堂练习 输入一段文字(100字内),分别存到数组s中。 使用list输出; 文字反向输出到label中 查找是否存在某个文字。

6 程序演示

7 一些提示 下标越界。 如何实现查找? 当使用不存在的数组下标。 在循环中一一比较。 For i = 1 To 100
If key = s(i) Then n = True End If Next i If n = True Then Label1.Caption = "找到了"

8 拓展 如何记录所有的查找结果? 如何记录字符的真实总数,而不是直接循环到100? 使用字符串累加 For i = 1 To 100
If key = s(i) Then n = n + 1 log = log & i & "," End If Next i If n > 0 Then Label1.Caption = "找到了,位置是:" & log Else Label1.Caption = "没有找到!"

9 小结 使用数组会让程序变得简单,而且避免了定义多个变量的麻烦。


Download ppt "3.16 枚举算法及其程序实现 ——数组的作用."

Similar presentations


Ads by Google