您的位置:首页技术文章
文章详情页

jsp实现剪子石头布小游戏

【字号: 日期:2022-06-07 15:11:10浏览:30作者:猪猪
导读:这篇文章主要为大家详细介绍了jsp实现剪子石头布小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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 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 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 src="<%=pathnew %>/images/<%=reslong %>"> <span color="red">vs</span> 电脑出的是:<img 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局哪!老师还提过下次要写个类似老虎机的游戏,估计在这个程序上做作弊就可以了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签: JSP
相关文章: