色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

JSP實現(xiàn)百萬富翁猜數(shù)字游戲

瀏覽:6日期:2022-06-07 14:37:42

本文實例為大家分享了JSP實現(xiàn)百萬富翁猜數(shù)字游戲的具體代碼,供大家參考,具體內(nèi)容如下

設(shè)計一個web app,每次產(chǎn)生一個30以內(nèi)的數(shù)字,給5次機會讓客戶猜測這個數(shù)字:

1)如果客戶猜的數(shù)字比產(chǎn)生的數(shù)字值大,則提示“大了”。
2)如果客戶猜的數(shù)字比產(chǎn)生的數(shù)字值小,則提示“小點”

猜對了就過關(guān),猜錯Game Over,給玩家重玩的機會。

JSP代碼:

<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<% 
 String result=(String)request.getAttribute("result");
 if(result!=null){
  out.write("<font color="red">"+result+""</font>");
 }
%>

<% 
 Integer times=(Integer)request.getAttribute("times");
 if(times!=null){
  out.write("你還有"+(5-times)+"次機會!");
 }
%>
<br/>
<form action="/zxz/zxz" method="POST">
 請輸入你的數(shù)(20以下):<input type="text" name="Lucy" /><br/>
 <%
 if(times!=null){
 %>
  <input type="hidden" name="times" value="<%=times %>"/>
 <% 
 }
 %>
 <input type="submit" value="競猜" />
</form>
</body>
</html>

Servlet代碼:

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;


/**
 * Servlet implementation class zxz
 */
@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("游戲結(jié)束<a href=""+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);
 }

}

總結(jié):

a. 使用標(biāo)簽hidden可以隱式傳遞數(shù)據(jù)而不被用戶發(fā)現(xiàn) 可以用來記錄次數(shù) 如:

<input type="hidden" name="times" value="<%=times %>"/>

b. Servlet是用來跳轉(zhuǎn)和執(zhí)行邏輯代碼的,JSP是用來展示數(shù)據(jù)的
c. request.getParameter(“Lucy”);如果參數(shù)不存在則返回null的字符串值
d 跳轉(zhuǎn)有兩種方式 一個是頁面跳轉(zhuǎn) 地址要寫項目名+jsp或者servlet

另一個是轉(zhuǎn)發(fā)共享了request的域?qū)ο?/strong>,地址可以直接寫jsp或者servlet 不要項目名 而且項目名和jsp或者servlet前都要加“/” 不然就是相對位置了

如:

<form action="/zxz/zxz" method="POST">
//轉(zhuǎn)發(fā)
request.getRequestDispatcher("/One.jsp"). 
forward(request, response);

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持。

標(biāo)簽: JSP
主站蜘蛛池模板: 日本三级成人中文字幕乱码 | 久久一本精品久久精品66 | 黄色成人免费观看 | 一本色道久久综合亚洲精品加 | 亚洲伦乱 | 东京一区二区三区高清视频 | 最新最好看免费毛片基地 | 久草手机在线观看 | 国产妇乱子伦视频免费 | 久久88香港三级台湾三级中文 | 国产日产久久高清欧美一区 | 国产高清自拍一区 | 国产精品91在线播放 | 久综合| 成人免费在线视频 | 亚洲国产老鸭窝一区二区三区 | 国产高清在线视频 | 最新三级网址 | 成人午夜视频一区二区国语 | 欧美一级香蕉毛片 | 女高中生被cao到哭视频 | 亚洲区精选网址 | 九九在线观看精品视频6 | 成人在线毛片 | 国产欧美17694免费观看视频 | 国产午夜亚洲精品一区网站 | 亚洲精品成人av在线 | 2020久久国产最新免费观看 | 久久免费激情视频 | 老外一级毛片免费看 | 日韩国产在线观看 | 国产一级久久免费特黄 | 国产欧美成人不卡视频 | 精品一区二区高清在线观看 | 国产精品区在线12p 国产精品人成 | 久久黄色免费网站 | 久久成人免费观看草草影院 | 成人毛片视频免费网站观看 | 丝袜精品 欧美 亚洲 自拍 | 亚洲精品成人中文网 | 国产一国产一级毛片视频在线 |