刚刚帮别人做了一个大作业,是写一个猜数字的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;
}
}
完毕。
分享到:
相关推荐
猜数字小游戏JAVA程序报告.doc
微信小游戏源码 猜数字小游戏源码(仅用于学习参考)微信小游戏源码 猜数字小游戏源码(仅用于学习参考)微信小游戏源码 猜数字小游戏源码(仅用于学习参考)微信小游戏源码 猜数字小游戏源码(仅用于学习参考)微信...
微信小游戏源码 猜数字小游戏源码2(仅用于学习参考)微信小游戏源码 猜数字小游戏源码2(仅用于学习参考)微信小游戏源码 猜数字小游戏源码2(仅用于学习参考)微信小游戏源码 猜数字小游戏源码2(仅用于学习参考)...
微信小游戏源码 猜数字小游戏源码1(仅用于学习参考)微信小游戏源码 猜数字小游戏源码1(仅用于学习参考)微信小游戏源码 猜数字小游戏源码1(仅用于学习参考)微信小游戏源码 猜数字小游戏源码1(仅用于学习参考)...
猜数字游戏 计算机产生一个随机数,猜中即胜,猜不中提示大了或者小了,继续猜,直到猜中为止。
html5微信小游戏源码 猜数字小游戏源码(仅用于参考)html5微信小游戏源码 猜数字小游戏源码(仅用于参考)html5微信小游戏源码 猜数字小游戏源码(仅用于参考)html5微信小游戏源码 猜数字小游戏源码(仅用于参考)...
猜数字小游戏,VB源码,与文曲星上的猜数字游戏玩法相同
html5微信小游戏源码 猜数字小游戏源码1(仅用于参考)html5微信小游戏源码 猜数字小游戏源码1(仅用于参考)html5微信小游戏源码 猜数字小游戏源码1(仅用于参考)html5微信小游戏源码 猜数字小游戏源码1(仅用于...
html5微信小游戏源码 猜数字小游戏源码2(仅用于参考)html5微信小游戏源码 猜数字小游戏源码2(仅用于参考)html5微信小游戏源码 猜数字小游戏源码2(仅用于参考)html5微信小游戏源码 猜数字小游戏源码2(仅用于...
基于Vue开发的猜数字小游戏源码(课程作业).zip基于Vue开发的猜数字小游戏源码(课程作业).zip基于Vue开发的猜数字小游戏源码(课程作业).zip基于Vue开发的猜数字小游戏源码(课程作业).zip基于Vue开发的猜数字小游戏...
自己模仿文曲星写的猜数字,都是刚学C#时候写的东西。
利用jsp web开发实现简单猜数字小游戏,主要有用户登录界面和猜数字功能
Android猜数字小游戏 源代码