首先使用JSP声明声明一个函数用于得到九九乘法表的内容
<%! String printMultiTable() { String s = ""; for (int i = 1; i < 10; i ++) { for (int j = 1; j <= i; j ++) s += " " + i + " * " + j + " = " + (i * j) + " "; s += "<br />"; } return s; } %>
然后在JSP脚本中使用它:
<%=printMultiTable() %>
完整的代码:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!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> <%! String printMultiTable() { String s = ""; for (int i = 1; i < 10; i ++) { for (int j = 1; j <= i; j ++) s += " " + i + " * " + j + " = " + (i * j) + " "; s += "<br />"; } return s; } %> 九九乘法表:<br /> <%=printMultiTable() %> </body> </html>
NineNineMultiply.jsp
效果:
时间: 2024-10-23 06:10:54