jsp實現(xiàn)剪子石頭布小游戲
本文實例為大家分享了jsp實現(xiàn)剪子石頭布游戲的具體代碼,供大家參考,具體內(nèi)容如下
老師前兩天除了一道小游戲的題目要大家做做,其實不太難,用了接近兩個小時才做出來,先看一下題目。
問題描述:實現(xiàn)兩個頁面,第一個頁面要求用圖片或radio或select,第二個頁面顯示輸贏的結(jié)果并把所有的結(jié)果保存輸出。剪子石頭布小游戲,跟常理一樣,不必多說。
實現(xiàn)過程:使用form表單進行跳轉(zhuǎn)處理,難點在圖片傳值這部分和數(shù)據(jù)統(tǒng)計部分,以下是代碼:
游戲界面代碼:
<html> <head> <base href="<%=basePath%>" > <title>歡迎來到剪刀石頭布游戲大廳</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" rel="external nofollow" > --> </head> <body> <!-- 獲得當(dāng)前web project的名字 --> <%String pa = request.getContextPath(); %> <!-- form表單頁面跳轉(zhuǎn) --> <form action="result.jsp" method="post"> <input type="radio" name="option" value="jiandao"><img alt="剪刀" src="<%=pa%>/images/jiandao.jpg"> <input type="radio" name="option" value="shitou"><img alt=石頭" src="<%=pa%>/images/shitou.jpg"> <input type="radio"name="option" value="bu"><img alt="布" src="<%=pa%>/images/bu.jpg"> <input type="submit"value="確定"/> </form> </body> </html>
游戲界面:
游戲結(jié)果頁面代碼:
<html> <head> <base href="<%=basePath%>" > <title>My JSP 'result.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" rel="external nofollow" > --> </head> <body> <% String pathnew=request.getContextPath(); //獲取游戲參與者的選項值 String res=request.getParameter("option"); String reslong=res+".jpg"; //產(chǎn)生隨機數(shù)進行匹配圖片 int com=(int)Math.random()*3; String computer=String.valueOf(com); //computer=0 jiandao //computer=1 shiyou //computer=2 bu if(computer.equals("0")){ computer="jiandao"; } else if(computer.equals("1")){ computer="shitou"; } else{ computer="bu"; } String computerlong=computer+".jpg"; int win=0; int lost=0; int ping=0; Object objwin=session.getAttribute("win"); Object objlost=session.getAttribute("lost"); Object objping=session.getAttribute("ping"); if(objwin==null){ session.setAttribute("win",String.valueOf(win)); } if(objlost==null){ session.setAttribute("lost",String.valueOf(lost)); } if(objping==null){ session.setAttribute("ping",String.valueOf(ping)); } %> <h3>結(jié)果是</h3> <!-- 圖片傳值 --> 您出的是:<img alt="" src="<%=pathnew %>/images/<%=reslong %>"> <span style="bold" color="red">vs</span> 電腦出的是:<img alt="" src="<%=pathnew %>/images/<%=computerlong %>"> <% //邏輯判斷,session更新統(tǒng)計值, if(res.equals(computer)){ out.println("平局!"); //session.setAttribute("ping",String.valueOf(Integer.valueOf((String)session.getAttribute("ping"))+1)); session.setAttribute("ping",String.valueOf(Integer.valueOf((String)session.getAttribute("ping"))+1)); } else if((res.equals("jiandao")&&computer.equals("bu"))||(res.equals("shitou")&&computer.equals("jiandao"))||(res.equals("bu")&&computer.equals("shiyou"))){ out.println("您贏了!"); //session.setAttribute("win",String.valueOf(Integer.valueOf((String)session.getAttribute("win"))+1)); session.setAttribute("win",String.valueOf(Integer.valueOf((String)session.getAttribute("win"))+1)); } else{ out.println("您輸了!"); session.setAttribute("lost",String.valueOf(Integer.valueOf((String)session.getAttribute("lost"))+1)); //session.setAttribute("lost",String.valueOf(Integer.valueOf((String)session.getAttribute("lost"))+1)); } %> <h3>統(tǒng)計結(jié)果,待寫入數(shù)據(jù)庫</h3> 您一共玩了<%=String.valueOf( Integer.valueOf((String)session.getAttribute("win"))+Integer.valueOf((String)session.getAttribute("lost"))+Integer.valueOf((String)session.getAttribute("ping")) )%>局<br/> 您贏了<%=String.valueOf( Integer.valueOf((String)session.getAttribute("win"))) %>局<br/> 您輸了<%=String.valueOf( Integer.valueOf((String)session.getAttribute("lost"))) %>局<br/> 打平了<%=String.valueOf( Integer.valueOf((String)session.getAttribute("ping"))) %>局<br/> </body> </html>
游戲結(jié)果:
這個絕對沒作弊,因為我輸了5局哪!老師還提過下次要寫個類似老虎機的游戲,估計在這個程序上做作弊就可以了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:jsp實現(xiàn)textarea中的文字保存換行空格存到數(shù)據(jù)庫的方法
欄 目:JSP編程
本文標(biāo)題:jsp實現(xiàn)剪子石頭布小游戲
本文地址:http://mengdiqiu.com.cn/a1/JSPbiancheng/11430.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的核心機制依賴注入
- 01-11jsp 使用request為頁面添加靜態(tài)數(shù)據(jù)的實
- 01-11Spring獲取ApplicationContext對象工具類的
隨機閱讀
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10C#中split用法實例總結(jié)