我们始终是路人

I'll try anything once.学习笔记

使用JSP技术实现百万富翁猜数字游戏

【使用JSP技术实现百万富翁猜数字游戏】
设计一个web app,每次产生一个30以内的数字,给5次机会让客户猜测这个数字:
1)如果客户猜的数字比产生的数字值大,则提示“可惜,大了点”。
2)如果客户猜的数字比产生的数字值小,则提示“可惜,小了点”
猜对了奖励100百万,猜错Game Over,给玩家重玩的机会。


guess.jsp(显示数据)
GuessServlet(java逻辑代码)


guess.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 'guess.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>
  <%
    String time=(String)session.getAttribute("time");
    String info=(String)session.getAttribute("info");
   %>
    <form action="/GuessGame/GuessServlet " method="get">
        <table border="true">
            <tr><td>请输入您要猜的数字:</td>
            <td><select name="guess">
                    <%for(int i=0;i<31;i++){%>
                        <option value="<%=i%>"><%=i%></option>
                    <%}%>
                </select>
            </td></tr>
            <tr><td><input type="submit" value="猜!"><input type="reset" value="重新选"><input type="submit" name="re" value="重新玩"></td>
            <td>注意:你还有<%=time %>次机会!</tr>
            <tr><td colspan="2">信息:<%=info %></td></tr>
        </table>
    </form>
  </body>
</html>

GuessServlet.java

package com.ning.guess;

import java.io.IOException;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class GuessServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        HttpSession session = request.getSession();
        if (request.getParameter("guess") == null || request.getParameter("re") != null) {// 第一次
            session.setAttribute("time", "5");// 机会数
            session.setAttribute("info", "欢迎!!!");// 显示信息
            this.getServletContext().getRequestDispatcher("/guess.jsp").forward(request, response);
        } else {
            int guess = Integer.valueOf(request.getParameter("guess"));// 用户猜的数字
            String time = (String) session.getAttribute("time");// 用户猜的次数
            if (time.equals("5")) {// 用户第一次猜,产生随机数
                session.setAttribute("random", new Random().nextInt(31));
            }
            int random = (Integer) session.getAttribute("random");
            Integer times = Integer.valueOf(((String) session.getAttribute("time")));
            if (times > 0) {
                times--;
                session.setAttribute("time", times.toString());
                if (guess > random) {
                    session.setAttribute("info", "可惜,大了点");
                    this.getServletContext().getRequestDispatcher("/guess.jsp").forward(request, response);
                } else if (guess < random) {
                    session.setAttribute("info", "可惜,小了点");
                    this.getServletContext().getRequestDispatcher("/guess.jsp").forward(request, response);
                } else {
                    session.setAttribute("info", "猜对啦,奖励100万");
                    session.setAttribute("time", "5");// 机会数
                    this.getServletContext().getRequestDispatcher("/guess.jsp").forward(request, response);
                }
            } else {
                session.setAttribute("info", "机会用完了,随机数为:" + random);
                this.getServletContext().getRequestDispatcher("/guess.jsp").forward(request, response);
            }
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        doGet(request, response);
    }

}

效果
这里写图片描述
这里写图片描述

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a1837634447/article/details/53699214
文章标签: jsp web app java
个人分类: JSP
想对作者说点什么? 我来说一句

jsp猜数字游戏

2014年11月09日 53KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭