首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

猜数目字小游戏

2012-11-23 
猜数字小游戏刚刚帮别人做了一个大作业,是写一个猜数字的MVC基本架构的小游戏。 时间:2小时。 难易程度:中

猜数字小游戏

刚刚帮别人做了一个大作业,是写一个猜数字的MVC基本架构的小游戏。

时间:2小时。
难易程度:中
难点:随机数的产生次数,以及技术功能的实现。

下面是代码:
shuzi.jsp

<%@ page language="java" pageEncoding="UTF-8"contentType="text/html;charset=UTF-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><html><head><title>猜数字</title></head><body><form method="post"action="${pageContext.request.contextPath}/Control"><br><br><br><br>?????????????????????????????????????????????????????<strong><font size="5">猜数字(请输入100以内的整数) </strong><br>${result }<table align="center" width="300" height="50" border="1"><tr><td align="center">?请输入数字:</td><td>?<input type="text" name="text1"></td></tr><tr><td align="center" colspan="2"><input type="submit" value="提交" name="button1"></td></tr></table></form><%request.setCharacterEncoding("UTF-8");%><c:choose><c:when test="${count>0}">您猜了${count }次</c:when></c:choose></font></body></html>

?

?fm.action包

Control.java

package fm.action;import java.io.IOException;import java.io.PrintWriter;import java.util.*;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import fm.service.caishu;import fm.service.random;public class Control extends HttpServlet {int count = 0;int random = 0;int num;String result = null;random r = new random();caishu c = new caishu();public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html;charset=utf-8");PrintWriter out = response.getWriter();request.setCharacterEncoding("UTF-8");doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html;charset=utf-8");PrintWriter out = response.getWriter();request.setCharacterEncoding("UTF-8");int cai = Integer.parseInt(request.getParameter("text1"));// 只生成一个随机数if (random == 0) {num = r.randomNum();System.out.println(num);random++;}result = c.caishu(cai, num,count);++count;request.setAttribute("count", count);request.setAttribute("result", result);request.getRequestDispatcher("./shuzi.jsp").forward(request, response);}}

?

fm.service包

caishu.java

package fm.service;import java.util.*;import java.io.*;public class caishu {String result;public String caishu(int cai, int a, int b) {if (cai > a) {result = "请输入小点的数";return result;} else if (cai < a) {result = "请输入大点的数";return result;} else {if (b <=2) {result = "猜对了,你真是个天才!";return result;} else if (b < 6) {result = "还将就,你过关了!";return result;} else if (b < 8 ) {result = "但是你还……真笨!";return result;} else {result = "你和猪没有两样了!";return result;}}}}

?random.java

package fm.service;import java.util.*;import java.io.*;public class random {public int randomNum() {Random a=new Random(); int num=a.nextInt(100); return num;}}

?

完毕。

热点排行