如答案: aryTKT(0)=10 aryTKT(1)=20 . . . aryTKT(9)=15 文本框输入内容: txtTKT(0)=20 txtTKT(1)=15 . . . txtTKT(9)=10我的要求是,9个文本框不按先后顺序输入答案。只要9个答案都答对就返回true 如: dim flg as Boolean if txtTKT(0)=aryTKT(0) and txtTKT(1)=aryTKT(1) and ... then flg=true elseif txtTKT(0)=aryTKT(1) and txtTKT(1)=aryTKT(0) and ...then flg=true . . . end if 但这样手动做起来很麻烦,想用一算法做起来简单点就行
function IsRightAnswer(aryTKT() as long,txtTKT as object) as boolean dim abIsMatch(9) as boolean, lMatchCount as long dim i as long, j as long, l as long for i= 0 to 9 l = CLng(txtTKT(i)) for j= 0 to 9 if (aryTKT(j) = l) and (not abIsMatch(j)) then abIsMatch(j) = true lMatchCount = lMatchCount+1 exit for end if next next IsRightAnswer = (lMatchCount = 10) end function
只有10个答案,算法上就没有深究了 Dim Re As Boolean '标记十个答案是否都对 Re = True '默认为全部正确 Dim tmpRe As Boolean '标记某个文本框的内容是否在答案中 Dim i As Integer, j As Integer
For i = 0 To 4 tmpRe = False For j = 0 To 4 If txtTKT(i) = aryTKT(j) Then tmpRe = True '找到相符的答案 Exit For '退出内层循环 End If Next j If Not tmpRe Then Re = False '有一个答案不正确 Exit For '后面的答案不需要再判断,直接退出外层循环 End If Next i
If Re Then MsgBox "十个答案全部正确" Else MsgBox "其中有错误答案" End If 楼主注意上述的代码其实还是有漏洞的,拿你上面举的例子来说,如果10个文本框里都输入20,还是会认为全部正确。 最严密的做法应该是把两个数组都排一下序,然后按顺序比对一遍就行了.
aryTKT(0)=10
aryTKT(1)=20
.
.
.
aryTKT(9)=15
文本框输入内容:
txtTKT(0)=20
txtTKT(1)=15
.
.
.
txtTKT(9)=10我的要求是,9个文本框不按先后顺序输入答案。只要9个答案都答对就返回true
如:
dim flg as Boolean
if txtTKT(0)=aryTKT(0) and txtTKT(1)=aryTKT(1) and ... then
flg=true
elseif txtTKT(0)=aryTKT(1) and txtTKT(1)=aryTKT(0) and ...then
flg=true
.
.
.
end if
但这样手动做起来很麻烦,想用一算法做起来简单点就行
dim abIsMatch(9) as boolean, lMatchCount as long
dim i as long, j as long, l as long
for i= 0 to 9
l = CLng(txtTKT(i))
for j= 0 to 9
if (aryTKT(j) = l) and (not abIsMatch(j)) then
abIsMatch(j) = true
lMatchCount = lMatchCount+1
exit for
end if
next
next
IsRightAnswer = (lMatchCount = 10)
end function
Dim Re As Boolean '标记十个答案是否都对
Re = True '默认为全部正确
Dim tmpRe As Boolean '标记某个文本框的内容是否在答案中
Dim i As Integer, j As Integer
For i = 0 To 4
tmpRe = False
For j = 0 To 4
If txtTKT(i) = aryTKT(j) Then
tmpRe = True '找到相符的答案
Exit For '退出内层循环
End If
Next j
If Not tmpRe Then
Re = False '有一个答案不正确
Exit For '后面的答案不需要再判断,直接退出外层循环
End If
Next i
If Re Then
MsgBox "十个答案全部正确"
Else
MsgBox "其中有错误答案"
End If
楼主注意上述的代码其实还是有漏洞的,拿你上面举的例子来说,如果10个文本框里都输入20,还是会认为全部正确。
最严密的做法应该是把两个数组都排一下序,然后按顺序比对一遍就行了.