Multiplication table?

Multiplication table?

本文关键字:table Multiplication      更新时间:2023-09-26

我需要编写两个具有以下属性的网页:

  • 表单(决定这应该是html还是jsp文件)
    表单包含一个包含两个字段的HTML表单:一个文本输入(称为"大小")和一个按钮

单击按钮时,会出现另一个页面。。

  • 表(决定这是HTML还是JSP)
    表显示了直到上一页中的"大小"的多规格表

示例:
如果点击3,则输出为:

1 2 3
2 4 6
3 6 9

此外,在"表格"中添加一个按钮,这样按下此按钮将使表格消失。

这是我的表单代码

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
    <h1>multiplication table</h1>
    <form action="form_action.jsp" method="get">
        Size: <input type="size" name="size" size="35" /><br /> 
        <input type="submit" value="Submit" />
    </form>
    <p>Click on the submit button, and the input will be sent to a page
        on the server called "form_action.jsp".</p>
</body>
</html>

和我的页面生成多规格表

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<head>
<title>Calculation from a form</title>
</head>
<body>
    <div>Calculation</div>
    <table border="Calculation">
        <%
            String temp = request.getParameter("number");
            int x = Integer.parseInt(temp);
            String table = "<table border='1' id='mytable'>";
            for (int row = 1; row < 11; row++) {
        %>
        <tr>
            <%
                for (int column = 1; column < 11; column++) {
            %>
            <td><tt><%=row * column%></tt></td>
            <%
                }
            %>
        </tr>
        <%
            }
        %>
    </table>
</body>

有人能帮我开始吗?

在您的第一个页面中,您已经输入了命名大小为<input type="size" name="size" size="35" />的值,但在form_action.jsp中,您正试图从number 中获取值

String temp = request.getParameter("number");

将其更改为size

String temp = request.getParameter("size");

您正在将该值解析为int x,但从未在for s中使用过它。如果更正该值,它就可以了。

这就是您想要的:form_action.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
    <h1>multiplication table</h1>
    <table border="1">
        <%
            int size = Integer.valueOf(request.getParameter("size"));
            for (int row = 1; row <= size; row++) {
            %>
            <tr>
            <%
                for (int column = 1; column <= size; column++) {
                    %>
                    <td><%=row*column %></td>
                    <%
                }
            %>
            </tr>
            <%
            }
        %>
    </table>
</body>
</html>