gpt4 book ai didi

java - 单击“下一步”按钮后,JSP 测验不会进入下一个问题

转载 作者:行者123 更新时间:2023-12-02 09:01:04 25 4
gpt4 key购买 nike

我正在 JSP 中构建一个测验,一次显示一个问题和四个答案,所有答案都是从 MySQL 数据库获取的。目前,使用以下代码,页面能够显示第一个问题并允许用户通过单选按钮提交答案。但是,单击“下一步”按钮后,页面刷新后仍显示问题一。我希望它显示下一个问题,然后是第二个问题,然后是第三个问题,依此类推。我该怎么做?

如果可能的话,有人可以为我编写代码而不只是解释它吗?

此外,我非常清楚使用 scriptlet 并不是一件好事,但是,我还远远不够先进,无法做其他事情。对于代码困惑表示歉意,我不得不匆忙这个项目,因为我只有一周的时间来完成它。

Connection conn = null;
ResultSet rs = null;
Statement st = null;

String action;
Scanner input = new Scanner(System.in);

int score = 0;
int count = 0;
int QID = 1;


try {
Class.forName("org.mariadb.jdbc.Driver");
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(
"jdbc:mariadb://ebs-db.eastbarnetschool.com//DETAILS);
System.out.println("Connection made");


CallableStatement stmt = conn.prepareCall("{call GetQuestionTitle(?, ?)}");
stmt.setInt(1, QID);
stmt.registerOutParameter(2, Types.VARCHAR);
stmt.execute();
String description = stmt.getString(2);
System.out.println(description);
%>
<%

CallableStatement answer1 = conn.prepareCall("{call GetAnswer1(?, ?)}");
answer1.setInt(1, QID);
answer1.registerOutParameter(2, Types.VARCHAR);
answer1.execute();
String answerOne = answer1.getString(2);
System.out.println(answerOne);

CallableStatement answer2 = conn.prepareCall("{call GetAnswer2(?, ?)}");
answer2.setInt(1, QID);
answer2.registerOutParameter(2, Types.VARCHAR);
answer2.execute();
String answerTwo = answer2.getString(2);
System.out.println(answerTwo);

CallableStatement answer3 = conn.prepareCall("{call GetAnswer3(?, ?)}");
answer3.setInt(1, QID);
answer3.registerOutParameter(2, Types.VARCHAR);
answer3.execute();
String answerThree = answer3.getString(2);
System.out.println(answerThree);

CallableStatement answer4 = conn.prepareCall("{call GetAnswer4(?, ?)}");
answer4.setInt(1, QID);
answer4.registerOutParameter(2, Types.VARCHAR);
answer4.execute();
String answerFour = answer4.getString(2);
System.out.println(answerFour);

%>
<%
String chosenAnswer=" ";
if(request.getParameter("button")!=null)
{
chosenAnswer=request.getParameter("button").toString();
}

CallableStatement stmt2 = conn.prepareCall("{call GetCorrectAnswer(?, ?)}");
stmt2.setInt(1, QID);
stmt2.registerOutParameter(2, Types.VARCHAR);
stmt2.execute();
String CorrectDescription = stmt2.getString(2);
System.out.println("\nCorrect Answer: " + CorrectDescription);

%>
<br>
<form name="Quiz" method="get" action='Quiz.jsp'>
<br>
<center>
<table border="1" width="500px" bgcolor="lightblue" cellspacing="0" cellpadding="0">
<tr>
<td width="100%">
<h1 align="center"><font color="white" face="arial">Quiz</font></h1>
<table border="0" width="500px" cellspacing="2" cellpadding="6">
<tr>
<td width="50%"><font color="steelblue" face="arial" size=4><span style="font-weight:normal"> QUESTION <%=QID%></span></font></td>
<tr>
<td width="100%"><font color="black" face="arial" size=4><span style="font-weight:normal"><%=description%></span></font></td></tr>
<tr>
<td>
1: <input type="radio" name="button" value= "<%=answerOne%>" /><font face="arial" size=3><%=answerOne%></font></td>
<tr>
<td>
2: <input type="radio" name="button" value="<%=answerTwo%>" /><font face="arial" size=3><%=answerTwo%></font></td>
<tr>
<td>
3: <input type="radio" name="button" value="<%=answerThree%>" /><font face="arial" size=3><%=answerThree%></font></td>
<tr>
<td>
4: <input type="radio" name="button" value="<%=answerFour%>" /><font face="arial" size=3><%=answerFour%></font></td>
<tr><td><center>

<%
%>


<input type="submit" value="Next" name="next">

</center></td></tr>
</table>
</td>
</tr>
</table>
</center>

<%
if(request.getParameterValues("button") != null) {
if(chosenAnswer.equals(CorrectDescription)) {
score = score + 1;
%>
<h4 align="center"><font color="green" face="arial">You got Question <%=QID-1%> correct!</font></h4>
<%
}
else{
%>
<h4 align="center"><font color="red" face="arial">You got Question <%=QID-1%> incorrect!</font></h4>
<%
}
}
%>
<input name="QID" type="HIDDEN" value="<%=QID+1%>" id="thisField">
</form>
<%
} catch(Exception e){
System.out.println(e.getMessage());

} finally {
if (rs != null) rs.close();
if (st != null) st.close();
if (conn != null) conn.close();
}

最佳答案

我假设(我自己没有做任何 JSP)

  1. 删除ONCLICK = <%=QID=QID+1%>>因为它没有做任何有用的事情

  2. 更改 int QID=1

<小时/>
int QID=1; 
if(request.getParameter("QID")!=null) {
QID=Integer.parseInt(request.getParameter("QID"));
}

这会让

<input name="QID" type="HIDDEN" value="<%=QID+1%>" id="thisField">

做好工作

您需要代码来避免超出可用问题的数量

关于java - 单击“下一步”按钮后,JSP 测验不会进入下一个问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60147768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com