vb编写一个随机抽取单词的程序
发布网友
发布时间:2022-11-03 08:58
我来回答
共5个回答
热心网友
时间:2023-10-30 19:07
首先你设计数据库,做一个单词表Words,字段ID 自动增量、Word字符型、Flag整型;ID是用来做单词标识,做主键里面数字不重复,Flag是用来做标志 0表示未取过,1表示已取过。
Dim CN As ADODB.Connection
Private Sub Command1_Click()
Dim RS As ADODB.Recordset
Dim tSQL As String
Dim sNum As Single
Dim iNum As Single
Dim I As Single
tSQL = "Select Count(*) From Words"
'初始化数据表
Set RS = New ADODB.Recordset
RS.Open tSQL, CN, adOpenStatic, adLockOptimistic
sNum = RS.Fields(0)
If sNum > 0 Then
Set RS = Nothing
Randomize
iNum = Int(Rnd * sNum + 1)
'根据状态随机抽取单词ID号
tSQL = "Select Word From Words Where ID=" & iNum & " And Flag=0"
Set RS = New ADODB.Recordset
RS.Open tSQL, CN, adOpenStatic, adLockOptimistic
'显示单词
Label1.Caption = RS.Fields(0)
'设置单词标识,取过后改为1
CN.Execute "Update Words Set Flag=1 Where ID=" & iNum
CN.Execute "Commit"
'关闭数据表
Set RS=Nothing
End If
End Sub
Private Sub Form_Load()
Set CN = New ADODB.Connection
'连接数据库
CN.Open "Driver={Microsoft Access Driver (*.mdb);Uid=Admin;Pwd=数据库密码;DBQ=数据库名称.mdb}"
'初始化单词表,标识都改0
CN.Execute "Update Words Set Flag=0"
CN.Execute "Commit"
End Sub
Private Sub Form_Terminate()
'关闭数据库
Set CN = Nothing
End Sub
热心网友
时间:2023-10-30 19:08
1 定义一个单词数组(0~N),N就是你要变换的单词的数目
2 将你的变数字范围确定在N的范围之内
3 窗体填加1个Label,2个按钮,1个定时器(用于不断变换,避免利用循环语句造成系统响应迟钝)
代码如下:
Option Explicit
Dim strWord() As String '单词数组
Dim lNum As Long '单词的个数
Private Sub Command1_Click() '开始或继续随机显示按钮
Timer1.Interval = 200
Timer1.Enabled = True
End Sub
Private Sub Command2_Click() '停止或选择按钮
Timer1.Enabled = False
End Sub
Private Sub Form_Load()
lNum = 9
ReDim strWord(0 To lNum - 1)
strWord(0) = "this"
strWord(1) = "hello"
strWord(2) = "message"
strWord(3) = "today"
strWord(4) = "thank"
strWord(5) = "May"
strWord(6) = "internal"
strWord(7) = "selection"
strWord(8) = "choice"
End Sub
Private Sub Timer1_Timer()
Dim tp As Long
Randomize
tp = Int(Rnd() * lNum)
Label1.Caption = strWord(tp)
End Sub
热心网友
时间:2023-10-30 19:08
Private Sub Command1_Click()
Randomize
Cls
a = Array("one", "two", "three", "free", "feel", "fly")
x = Int(Rnd * (UBound(a) + 1))
Print a(x)
End Sub
热心网友
时间:2023-10-30 19:09
给你的单词 进行数组编号
按照随即编号来找单词就可以
热心网友
时间:2023-10-30 19:10
有区别吗?