2012-10-21 19 views
0

我正在開發一個基於普通JSP-MsAccess connection進行小測驗的網頁。但是當我運行測驗並回答問題時,結果將應用於所有問題也就是說,如果我得到的第一個問題右然後我的分數是10,如果我會首先回答錯了我的分數是0 代碼:jsp中的選擇題

<%@ page import="java.sql.*" %> 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"> 

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>JSP Page</title> 
</head> 
<body> 
<% 
String ans=" "; 
if(request.getParameter("correctAns")!=null) 
{ 
    ans=request.getParameter("correctAns").toString(); 
} 


Connection conn = null; 

Statement st = null; 

ResultSet rs = null; 

String id=request.getParameter("id"); 
System.out.println("id:"+id); 

int i=1; 

String s,g; 

int count=0; 

try { 

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
conn = DriverManager.getConnection("jdbc:odbc:qdsn"); 
rs = null; 

st = conn.createStatement(); 

//for(i=1;i<=2;i++) 
// { 
rs = st.executeQuery("select * from exam"); 

while(rs.next()) { 
%> 


<br/> 
<center> 

<table border="1" width="500px" bgcolor="pink" cellspacing="0" cellpadding="0"> 
    <tr> 
    <td width="100%"> 

<form name="form1"> 

<h2 align="center"><font color="red">Online Quiz Application</font></h2> 

<b>Select Correct Answer</b> 
     <table border="0" width="500px" cellspacing="2" cellpadding="4"> 
<tr> 

<td width="50%"> Question:</td> 
<input type="hidden" name="correctAns" value="<%=rs.getString(6)%>" /> 
<tr> 
<td><%= rs.getString("quest") %></td></tr> 
<tr> 
<td> 

1: <input type="radio" name="a" value= "QA" /></td> 
    <td><%= rs.getString("QA") %></td></tr> 
<tr> 
<td> 
2: <input type="radio" name="a" value="QB" /></td> 
<td><%= rs.getString("QB") %></td></tr> 

<tr> 
<td> 
3: <input type="radio" name="a" value="QC" /></td> 
<td><%= rs.getString("QC") %> </td></tr> 

<tr> 
<td> 
4: <input type="radio" name="a" value="QD" /> </td> 
<td> <%= rs.getString("QD") %> </td></tr> 

<tr> 
<td> 
<center> 
    <input type="submit" value="Submit" name="submit"></center></td></tr> 
</table> 

</form> 
</td> 
    </tr> 
</table> 
</center> 
</body> 
<% g=request.getParameter("a"); 
%> 
<% 
if(g.equals(ans)){ 

count++; 
} 

%> 



<% 
}} 
// } 
catch (Exception ex) { 
ex.printStackTrace(); 

%> 

<% 
} finally { 
if (rs != null) rs.close(); 
if (st != null) st.close(); 
if (conn != null) conn.close(); 
} 
out.println("Score="+count); 
%> 


</html> 
+0

你的問題是不對的。 – cherit

回答

1

更新您的單選按鈕的名稱使用一些指數即

在頂端:聲明索引變量。

<% int qNum = 0; %> 

在頁面中間,使用索引變量radio的字段命名。

<input type="radio" name="a<%=qNum%>" value="QA" /> 
<input type="radio" name="a<%=qNum%>" value="QB" /> 
<input type="radio" name="a<%=qNum%>" value="QC" /> 
<input type="radio" name="a<%=qNum%>" value="QD" /> 

在頁面底部,檢索索引參數並將索引增加1。

<% g=request.getParameter("a"+qNum); 
    qNum++; 
    %>