我有以下代码quiz.jsp:
<form action= "execute.jsp" method="post" >
<table>
<%
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql....);
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("Select * from questions");
int i=1;
while(rs.next()){
int in=rs.getInt(1);
%>
<tr>
<td>
<%=i%><p id="test"><%=rs.getString("questions")%></p>
</td>
</tr>
<tr id="two">
<td>
<input type="radio" value=<%=rs.getString("answerone")%> name="radio<%=in%>" id="a"/><%=rs.getString("answerone")%>
<input type="radio" value=<%=rs.getString("answertwo")%> name="radio<%=in%>" id="b"/><%=rs.getString("answertwo")%>
<input type="radio" value=<%=rs.getString("answerthree")%> name="radio<%=in%>" id="c"/><%=rs.getString("answerthree")%>
<input type="radio" value=<%=rs.getString("answerfour")%> name="radio<%=in%>" id="d"/><%=rs.getString("answerfour")%>
</td>
</tr>
<%
i++;
}
}catch(Exception ex){
out.print("Exception Caught"+ex);
}
%>
</table>
<div id="buttons">
<input type="submit" value="submit"></input>
</div>
</form>
</div>
execute.jsp
<%!
int calculatePercentage(int a,int b)
{
int result=(a/b) * 100;
return result;
}
%>
<%
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql:......);
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("Select * from questions");
int j=0;
String[] correct=new String[4];
while(rs.next()){
j++;
correct[j]=rs.getString("iscorrect");
//out.println(correct[j]);
}
String check[]=new String[4];
for(int x=1; x<4; x++){
check[x]=request.getParameter("radio"+x);
//out.print(check[x]);
}
int count=0;
for(int a=0;a<correct.length;a++){
if((correct[a]!=null)&&(check[a]!=null)){
if(correct[a].equals(check[a])){
out.println(correct[a]);
out.println(check[a]);
count++;
}
}
}
int max=3;
int score=calculatePercentage(count,max);
out.println("Your "+score+" answers are correct");
// out.println("Your percentage scored is: "+score +"%");
}catch(Exception ex){
out.print("Exception Caught"+ex);
}
%>
问题:
我遇到的问题是,如果所有答案都是正确的,那么测试分数是准确的(100%) 如果没有选择其中一个无线电 如果一个或多个错误
结果为0%。我看起来计数没有正确传递值或方法执行不好。虽然如果我执行out.print(count),它会显示正确的计数。
请建议。
由于