jsp打印杨辉三角

//jsp打印杨辉三角
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
 										  杨辉三角 <br>
    <%
    int[][] a; int flag = 20; a = new int[20][20]; a[0][0] = a[1][0] = a[1][1] = 1; for ( int i = 2; i
										      < 20; i++ )
{
	for ( int j = 0; j < 20; j++ )
	{
		if ( j >= 1 )
		{
			a[i][0] = 1; a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
		}
	}
}
for ( int i = 0; i < 20; i++ )
{
	for ( int p = flag; p > 0; p-- )
	{
		out.print( "&nbsp;" );
	}
	for ( int j = 0; j < 20; j++ )
	{
		if ( a[i][j] > 0 )
		{
			out.print( "&nbsp;" + a[i][j] );
		}
	}
	out.print( "<br> " ); flag--;
}
%>
  </body>
</html>

jsp打印杨辉三角(myeclipse 10 +jdk1.8.0 +tomcat )

刚开始也是不太明白,应该怎么才能把 java 代码嵌入到 html 里,通过百度知道 

需要 <%   java代码  %>

然后是怎么样才能把输出打印到网页上,也是百度了一下

可以直接通过 out.printlin(” “)  只不过  想要打印空格得需要  &nbsp

要是想要换行需要使用H5强制换行符 <br>

都弄好了后,就打开tomcat服务:

《jsp打印杨辉三角》

图上是我的已经打开了。

然后就可以把你的项目加载到服务里:

《jsp打印杨辉三角》

然后可以直接通过myeclipse 客户端查看网页效果,也可以通过浏览器进行访问啦

一般网址格式:http://localhost:8080/test/index.jsp

显示你的ip:tomcat默认网端/你的项目名/二级子目录……/最后是你的访问目标网页

jsp打印杨辉三角

    原文作者:杨辉三角问题
    原文地址: https://blog.csdn.net/qq_26816591/article/details/52550710
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞