zoukankan      html  css  js  c++  java
  • 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 'game.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>
        <%!int count = 0;%>
    
        <%
            Object obj = session.getAttribute("numint");//第一次访问的时候,为null,第二次访问之后,都有值
            if (obj == null) {
                Random r = new Random();
                obj = r.nextInt(100);
                session.setAttribute("numint", obj);
            }
            int rm = Integer.parseInt(obj.toString());
        %>
    
        <%
            String numstr = request.getParameter("num");//获取猜数的数字
            int cou=0;
            if (numstr != null && !"".equals(numstr)) {//直接浏览器url访问jsp,num值为null,提交的时候,没有输入num的值,num值为""
                //num肯定是有值
                count++;
                cou=count;
                int num = Integer.parseInt(numstr);
                if (num > rm) {
                    out.println("太大了");
    
                } else if (num < rm) {
                    out.println("太小了");
                } else {
                    out.println("终于猜正确了");
                    Random r = new Random();
                    obj = r.nextInt(100);
                    session.setAttribute("numint", obj);
                    count=0;
                }
                
            }
        %>
    
    
        欢迎您玩猜数游戏,猜了
        <%=cou%><%--<%out.print(rm); --%>
        <form action="game.jsp" method="post">
            <br> 输入数字 <input name="num"> <input type="submit"
                value="点击确认">
        </form>
    
      </body>
    </html>

    运行截图:

  • 相关阅读:
    SQLSERVER 对于非dbo的表增加注释
    关于Dos命令中存在中文的解决方法
    NHibernate 映射关系
    解决SVN图标不显示问题
    SqlServer使用表值函数汇总
    【编程练习】poj1068
    【编程练习】poj1111
    大数据处理的一些总结和应用(有关舆情监控)
    leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题
    Leetcode 4 Median of Two Sorted Arrays
  • 原文地址:https://www.cnblogs.com/a1045417817/p/7582465.html
Copyright © 2011-2022 走看看