JSP实现百万富翁猜数字游戏(jsp教程网站)硬核推荐

随心笔谈2年前发布 编辑
167 0
🌐 经济型:买域名、轻量云服务器、用途:游戏 网站等 《腾讯云》特点:特价机便宜 适合初学者用 点我优惠购买
🚀 拓展型:买域名、轻量云服务器、用途:游戏 网站等 《阿里云》特点:中档服务器便宜 域名备案事多 点我优惠购买
🛡️ 稳定型:买域名、轻量云服务器、用途:游戏 网站等 《西部数码》 特点:比上两家略贵但是稳定性超好事也少 点我优惠购买


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);
}

}

© 版权声明

相关文章