本文实例为大家分享了jsp实现剪子石头布游戏的具体代码,供大家参考,具体内容如下
老师前两天除了一道小游戏的题目要大家做做,其实不太难,用了接近两个小时才做出来,先看一下题目。
问题描述:实现两个页面,第一个页面要求用图片或radio或select,第二个页面显示输赢的结果并把所有的结果保存输出。剪子石头布小游戏,跟常理一样,不必多说。
实现过程:使用form表单进行跳转处理,难点在图片传值这部分和数据统计部分,以下是代码:
游戏界面代码:
<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> <!-- 获得当前web project的名字 --> <%String pa = request.getContextPath(); %> <!-- form表单页面跳转 --> <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>
游戏界面:
游戏结果页面代码:
<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"; //产生随机数进行匹配图片 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>结果是</h3> <!-- 图片传值 --> 您出的是:<img alt="" src="<%=pathnew %>/images/<%=reslong %>"> <span style="bold" color="red">vs</span> 电脑出的是:<img alt="" src="<%=pathnew %>/images/<%=computerlong %>"> <% //逻辑判断,session更新统计值, 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>统计结果,待写入数据库</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>
游戏结果:
这个绝对没作弊,因为我输了5局哪!老师还提过下次要写个类似老虎机的游戏,估计在这个程序上做作弊就可以了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持尊托云数。
来源:DeepenStudy 漏洞文件:js.asp % Dimoblog setoblog=newclass_sys oblog.a...
CKeditor,以前叫FCKeditor,已经使用过好多年了,功能自然没的说。最近升级到3....
前言 项目开发中不管是前台还是后台都会遇到烦人的null,数据库表中字段允许空值...
本文实例讲述了AJAX+Servlet实现的数据处理显示功能。分享给大家供大家参考,具...
idea官方推送了2020.2.4版本的更新,那么大家最关心的问题来了,之前激活idea202...
本文转载自微信公众号「SH的全栈笔记」,作者SH。转载本文请联系SH的全栈笔记公...
本文转载自微信公众号「SQL数据库」,作者丶平凡世界 。转载本文请联系开发公众...
大家好,我是狂聊君。 今天来聊一聊 Mysql 缓存池原理。 提纲附上,话不多说,直...
问题:我们在做flex的开发中,如果用到别人搭建好的框架,而别人的server名称往...
在Flash Player 10.1及以上版本中,adobe新增了全局错误处理程序UncaughtErrorEv...