[英]How I create a program in JSP that produces random addition equations that you can answer which then checks to see if you're correct or not?
Let me preface this by saying this: I am an absolute beginner regarding JSP.让我先这样说:我是 JSP 的绝对初学者。 I'm still trying to understand the fundamentals while also get my work done in a timely manner without asking the instructor for help (thanks to them never returning emails when me and some other classmates have reached out regarding the work and things such as missing information or links).
我仍在努力了解基础知识,同时在不寻求教师帮助的情况下及时完成我的工作(感谢当我和其他一些同学就工作和诸如丢失信息之类的事情联系时,他们从不回复电子邮件或链接)。
I have been tasked to make a program in JSP that generates random addition problems.我的任务是用 JSP 编写一个程序,该程序会产生随机加法问题。 The user is able to input the answer they think is write and, after submitting them, will be told by the program which they've got right and wrong.
用户可以输入他们认为是写的答案,提交后,程序会告诉他们他们的对与错。 I've written a program thus far that, while it runs, it doesn't actually display anything in the browser.
到目前为止,我已经编写了一个程序,当它运行时,它实际上并没有在浏览器中显示任何内容。 It just constantly says "waiting for localhost'. Here is what I've made thus far:
它只是不断地说“等待本地主机”。这是我迄今为止所做的:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import = "java.util.*"%>
<>
<!DOCTYPE html>
<html>
<head>
<title>Addition Quiz</title>
</head>
<body>
<%
int a, b, answer;
Random rand = new Random();
a = rand.nextInt(1000);
b = rand.nextInt(1000);
%>
<form>
<%= a %> + <%= b %> =
<input type="number" value="answer" name="answer"><br><br>
<input type="submit" value="Submit">
</form>
<%
request.getParameter("answer");
answer = a + b;
if (answer==a+b) {
out.print("Correct");
} else if (answer!=(a+b)) {
out.print("Incorrect");
}
%>
</body>
</html>
What am I doing wrong here and, in your opinion, how should I go about it?我在这里做错了什么,在您看来,我应该怎么做? I'm using NetBeans and GlassFish.
我正在使用 NetBeans 和 GlassFish。
EDIT: Added to the program as per the suggestion of Swati.编辑:根据 Swati 的建议添加到程序中。 While it now outputs, the page will automatically say correct and whenever I submit a number to answer the equation the page just sorta refreshes to a new equation.
当它现在输出时,页面会自动说正确,每当我提交一个数字来回答等式时,页面就会刷新为一个新的等式。
As you submit it is reloading the page hence new random numbers are generating.Please find the below code.当您提交时,它正在重新加载页面,因此正在生成新的随机数。请找到以下代码。 index.html
索引.html
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<>
<!DOCTYPE html>
<html>
<head>
<title>Addition Quiz</title>
</head>
<body>
<%
int a, b, answer;
Random rand = new Random();
a = rand.nextInt(1000);
b = rand.nextInt(1000);
%>
<form action="result.jsp">
<%= a %>
+
<%= b %>
<%
request.setAttribute("a", a);
request.setAttribute("b", b);
%>
= <input type="number" value="answer" name="answer"><br>
<input type="hidden" name="a" value="<%=a%>">
<input type="hidden" name="b" value="<%=b%>">
<br><input type="submit" value="Submit">
</form>
</body>
</html>
result.html结果.html
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>answer is::<%=Integer.parseInt(request.getParameter("answer")) %></h1>
<h1>a+b is::<%=Integer.parseInt(request.getParameter("a"))+Integer.parseInt(request.getParameter("b")) %></h1>
<h1>RESULT IS <%= (Integer.parseInt(request.getParameter("answer")) == (Integer.parseInt(request.getParameter("a"))+Integer.parseInt(request.getParameter("b"))) )?"Correct":"InCorrect"%></h1>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.