package hah;
import java.io.IOException;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(“/zxz”)
public class zxz extends HttpServlet {
private static final long serialVersionUID=1L;
int answer;
public void newGame() {
Random random=new Random();
answer=random.nextInt(20);
}
public zxz() {
newGame();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(“text/html;charset=utf-8”);
String lucyStr=request.getParameter(“Lucy”);
Integer lucyNb=null;
System.out.println(“答案:”+answer);
if(!lucyStr.equals(“”)) {
lucyNb=Integer.parseInt(lucyStr);
}
Integer times=1;
String timeStr=request.getParameter(“times”);
if(timeStr!=null&&!timeStr.equals(“”)) {
times=Integer.parseInt(timeStr)+1;
}
if(times<5) {
String result=””;
if(lucyNb>answer) {
result=”大了”;
}else if(lucyNb<answer) {
result=”小了”;
}else if(lucyNb==answer) {
result=”中了”;
times=null;
}
request.setAttribute(“times”, times);
request.setAttribute(“result”, result);
}else {
newGame();
response.getWriter().write(“游戏结束<a href=’https://www.jb51.net/article/”+request.getContextPath()+”/One.jsp’>再来一把</a>”);
return;
}
request.getRequestDispatcher(“/One.jsp”).forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}