|
Spring Semester 2004
|
Here's another JSP from a while ago:
<% int count, right; String message = "Hello and welcome to the addition quiz!";
if (session.getAttribute("count") == null || request.getParameter("reset") != null) {
session.setAttribute("count", new Integer(0)); count = 0;
session.setAttribute("right", new Integer(0)); right = 0;
} else {
count = ((Integer)(session.getAttribute("count"))).intValue();
right = ((Integer)(session.getAttribute("right"))).intValue();
try {
if (Integer.parseInt(request.getParameter("answer")) == ((Integer)(session.getAttribute("answerKey"))).intValue())
right += 1;
else
right += 0;
} catch (Exception e) { }
count += 1;
message = right + " out of " + count;
if (count == 10) {
message = "Final result: " + message + ". New game started "; count = 0; right = 0;
} else
message = "Your performance thus far: " + message;
session.setAttribute("count", new Integer(count));
session.setAttribute("right", new Integer(right));
}
int one = (int) (Math.random() * 100 - 50),
two = (int) (Math.random() * 100 - 50);
session.setAttribute("answerKey", new Integer(one + two));
%>
<html>
<body>
<form action="<%=request.getContextPath() + request.getServletPath()%>">
<%=message%> <p>
Question <%=count+1%>. <%=one%> + <%=two%> = <input type="text" name="answer"> <p>
<input type="submit" value="Proceed"> <input type="submit" name="reset" value="Reset"> <p>
</form>
</body>
</html>
It is clean and compact, and a pleasure to study (I hope). (The long lines, you ask? Aye (for one) am really really happy it's so easy to use them on the web.)
A348/A548