简体   繁体   English

我如何在 JSP 中创建一个程序来生成您可以回答的随机加法方程,然后检查您是否正确?

[英]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.

相关问题 我在考试中遇到了Inheritance这个问题,并与output..你能告诉我正确答案和原因吗? - I came over this question of Inheritance in an exam and was confused with output.. can you tell me the correct answer and why 如何根据您所处的级别从列表中选择几乎随机的问题? - How can I pick almost random questions from a list depending on which level you are on? 你可以帮我做我的android加法程序吗? - can you help me with my android addition program? 一个加法程序,它会不断累加您输入的数字,直到您输入零为止。 在Java中 - An addition program which keeps adding up the numbers you input until you enter a zero. In Java 如何在jGraphx中回答鼠标事件? - How can you answer a mouse event in jGraphx? 我的程序缺少一步! 请看看您是否能找到我想念的东西? - My program is lacking a step! Please see if you can find what I am missing? 如何在其他活动中发送正确答案和所选答案? - How can i send the correct answer and the chosen answer in another activity? Java加法程序,用于对输入的数字进行累加,直到输入零为止。 SumPositive和SumNegative的平均值 - Java addition program which keeps addup the numbers you input ing until you enter a zero. Average of SumPositive and SumNegative Java Bean ::如何创建动态执行的方法(可以重新调整) - Java Bean :: How can you create a dynamically executed method (that can be re-adjusted) 如何在Java的yahtzee程序中重新滚动? - How do you do re-rolls in a yahtzee program in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM