include指令与动作分别嵌入阶乘和加法算法

  • 写一个页面求阶乘和求加法,阶乘使用include指令嵌入算法页面,加法使用include动作嵌入算法页面。

新建首页index.jsp,写好一些必要的布局

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
    <style>
      form {
        width: 800px;
        height: 300px;
        margin: 100px auto;
        border-radius: 10px;
        background-color: #ffaaff;
      }

      .container {
        width: 300px;
        height: 100px;
        position: relative;
        top: 20px;
      }

      span {
        font-size: 20px
      }

      .input-box {
        width: 200px;
        height: 30px;
        border-radius: 10px;
      }

      .container2 {
        height: 50px;
        padding-top: 20px;
      }
      .button{
        width: 150px;
        height: 40px;
        border-radius: 30px;
        background-color: #aaaaff;
      }
    </style>
  </head>
  <body>
  <form action="index.jsp" method="get">
    <div class="container">
      <span>求阶乘:</span>
      <input type="text" name="num" id="num" class="input-box">
      <div style="height: 30px;padding-top: 10px;font-size: 20px;"><%@ include file="include.jsp"%></div>
    </div>
    <div class="container2">
      <span>求加法:</span>
      <input type="text" name="num1" id="num1" class="input-box">+
      <input type="text" name="num2" id="num2" class="input-box">
      <div style="height: 30px;padding-top: 10px;font-size: 20px;"><jsp:include page="answer.jsp"></jsp:include></div>
    </div><br><br>
    <input type="submit" value="计算" class="button">
  </form>
  </body>
</html>

再建一个include.jsp(名字可以自取),用来写阶乘算法

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
  String s = request.getParameter("num");
  if (s==null){
    s="1";
  }
  int num=Integer.parseInt(s);
  double t=1;
  for (int i=1;i<=num;i++)
    t=t*i;
    out.print(num+"的阶乘为:"+t);
%>

再建一个answer.jsp,用来写加法算法

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    String o = request.getParameter("num1");
    Integer num1 = 0;
    if (o != null) {
        num1 = Integer.parseInt(o);
    }
    String h = request.getParameter("num2");
    Integer num2 = 0;
    if (h != null) {
        num2 = Integer.parseInt(h);
    }
    Integer result = num1 + num2;
    out.print("两数相加的结果为:"+result);

%>
</body>
</html>

运行结果:

 

 

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alan木子李

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值