JSP實現(xiàn)百萬富翁猜數(shù)字游戲
本文實例為大家分享了JSP實現(xiàn)百萬富翁猜數(shù)字游戲的具體代碼,供大家參考,具體內(nèi)容如下
設(shè)計一個web app,每次產(chǎn)生一個30以內(nèi)的數(shù)字,給5次機(jī)會讓客戶猜測這個數(shù)字:
1)如果客戶猜的數(shù)字比產(chǎn)生的數(shù)字值大,則提示“大了”。
2)如果客戶猜的數(shù)字比產(chǎn)生的數(shù)字值小,則提示“小點”
猜對了就過關(guān),猜錯Game Over,給玩家重玩的機(jī)會。
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)+"次機(jī)會!"); } %> <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í)有所幫助,也希望大家多多支持我們。
欄 目:JSP編程
下一篇:使用IDEA編寫jsp時EL表達(dá)式不起作用的問題及解決方法
本文標(biāo)題:JSP實現(xiàn)百萬富翁猜數(shù)字游戲
本文地址:http://mengdiqiu.com.cn/a1/JSPbiancheng/11416.html
您可能感興趣的文章
- 01-11在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法
- 01-11jsp文件下載功能實現(xiàn)代碼
- 01-11JSP頁面跳轉(zhuǎn)方法大全
- 01-11Spring獲取ApplicationContext對象工具類的實現(xiàn)方法
- 01-11jsp 使用request為頁面添加靜態(tài)數(shù)據(jù)的實例
- 01-11web前端超出兩行用省略號表示的實現(xiàn)方法
- 01-11JSP servlet實現(xiàn)文件上傳下載和刪除
- 01-11JSP狀態(tài)管理的簡單介紹
- 01-11jsp+servlet實現(xiàn)文件上傳與下載功能
- 01-11將properties文件的配置設(shè)置為整個Web應(yīng)用的全局變量實現(xiàn)方法


閱讀排行
本欄相關(guān)
- 01-11web下載文件和跳轉(zhuǎn)的方法
- 01-11Spring注入Date類型的三種方法總結(jié)
- 01-11在JSP中使用formatNumber控制要顯示的小
- 01-11Properties 持久的屬性集的實例詳解
- 01-11EJB3.0部署消息驅(qū)動Bean拋javax.naming.Na
- 01-11jsp文件下載功能實現(xiàn)代碼
- 01-11JSP頁面跳轉(zhuǎn)方法大全
- 01-11詳解Spring的核心機(jī)制依賴注入
- 01-11jsp 使用request為頁面添加靜態(tài)數(shù)據(jù)的實
- 01-11Spring獲取ApplicationContext對象工具類的
隨機(jī)閱讀
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10C#中split用法實例總結(jié)
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05織夢dedecms什么時候用欄目交叉功能?