注册 登录
编程论坛 ASP技术论坛

随即产生考试试题,这段循环代码看不懂,

Kettyjin1983 发布于 2007-11-14 14:53, 558 次点击

这段代码是这样的:红色的那一部分真是不懂啊。

Dim num(),k,i,x,j,ans()
redim num (totalq)
redim ans (totalq)

k = 1
Randomize

For i = 1 To totalq
x = Round(Rnd * totalrec)
'give record count
For j = 1 To k
If num(j) = x Then
i = i - 1
Exit For
End If
Next
If j > k Then
num(k) = x
k = k + 1
End If
Next

6 回复
#2
xiuit2007-11-14 22:29
NUM的值是什么呀,totalrec是从哪儿来的呀
#3
Kettyjin19832007-11-15 10:31

<%@ Language=VBScript %>
<%option explicit
%><!-- #INCLUDE FILE = "connect_ama.asp" --><%

dim myrs
set myrs = server.createobject("ADODB.recordset")
myrs.open "select * from all_exams where ExamName ='" & Request.Form ("start") & "'" ,myconn

''''''''''''''''''''''''''''''''''''''''''''''''''
dim q_id1, q_id2, code, totaltime, ppercent, totalq, expire, examname
q_id1 = myrs("sq_id")
q_id2 = myrs("eq_id")
totaltime = myrs("totalTime")
totalq = myrs("totalQuestions")
examname = myrs("ExamName")
ppercent = myrs("ppercentage")
code = myrs("code")
expire = myrs("expireDate")
myrs.Close
session("totalquestions")=totalq
''''''''''''''''''''''''''''''
myrs.open "select * from q_bank where q_id between " & q_id1 & " and " & q_id2 ,myconn,3
'3=adOpenStatic,then only myrs.recordcount will work
''''''''''''''''''''''''''''''
dim totalrec
totalrec = myrs.RecordCount
session.Contents("ExamName")=Request.Form ("start")
'Response.Cookies("totaltime")= cstr(totaltime)


'if myrs.Fields("ExpireDate") < Date then
'Response.Write "<h5>EXPIRED ''''''''<H5>"
'end if


' Response.Write "total time=" & totaltime & "<hr> total q=" & totalq & "<hr> total rec=" & totalrec &"<hr>"
Dim num(),k,i,x,j,ans()
redim num (totalq)
redim ans (totalq)

k = 1
Randomize

For i = 1 To totalq
x = Round(Rnd * totalrec)
'give record count
For j = 1 To k
If num(j) = x Then
i = i - 1
Exit For
End If
Next
If j > k Then
num(k) = x
k = k + 1
End If
Next
'''''''''''''''''''''''''''''''''''
for i=1 to totalq
ans(i)="Not Attempted"
'Response.Write i & " | " &num(i)& " | "
myrs.MoveFirst
myrs.Move num(i)-1
num(i)=myrs("q_id")
'Response.Write num(i) & " |<br> "
Next
Session("Questions")=num
Session("Answers")=ans
Session("Counter")=1
Session("examover")=0
Session ("examname")= examname
Session("ppercentage")= ppercent

'''''''''''''''''''''''''''''''''
'closing order important'''''''''
myrs.close
myconn.close
set myconn=nothing
set myrs=nothing
'''''''''''''''''''''''''''''''''

'Response.Redirect "exam_paper.asp"
Response.Cookies(examname & "totaltime")= cstr(totaltime)

%>

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
</HEAD>
<BODY>
<form name=examform action="exam_paper.asp" method=post>
<input type=hidden name=qno value=1>
<input type=hidden name=totalq value=<%=totalq%>>
</form>
<script language="JavaScript">
document.examform.submit()
</script>


</BODY>
</HTML>

#4
Kettyjin19832007-11-15 10:34

我把整段代码都贴上了,就是下面这段循环看不懂,帮帮我吧。
For i = 1 To totalq
x = Round(Rnd * totalrec)
'give record count
For j = 1 To k
If num(j) = x Then
i = i - 1
Exit For
End If
Next
If j > k Then
num(k) = x
k = k + 1
End If
Next

#5
multiple19022007-11-15 11:14

Dim num(),k,i,x,j,ans()
redim num (totalq)
redim ans (totalq)

k = 1
Randomize

For i = 1 To totalq
x = Round(Rnd * totalrec)
'give record count
For j = 1 To k
If num(j) = x Then ' num里的元素还没赋值吧,怎么能引用呢?
i = i - 1
Exit For
End If
Next
If j > k Then
num(k) = x
k = k + 1
End If
Next

#6
Kettyjin19832007-11-15 11:45
晕,也就是这段代码本身就有问题了。那怎么办啊,怎么改啊?
#7
Kettyjin19832007-11-15 13:10

运行是成功得,这是怎么回事啊?哪位大人帮我看看啊

1