宝哥软件园

jsp实现剪刀石头布游戏

编辑:宝哥软件园 来源:互联网 时间:2021-08-27

这个例子分享了jsp实现剪刀石头布游戏的具体代码,供大家参考。具体内容如下

老师前两天让大家做了一个小游戏的题目,但是并不太难。做这件事花了将近两个小时。先看题目。

问题描述:实现两页,第一页需要图片或单选或选择,第二页显示输赢结果并保存所有结果输出。剪刀石头布游戏,像常识一样,不用多说。

实现过程:利用表格形式进行跳转处理,难点在于图像值传输部分和数据统计部分。以下是代码:

游戏界面代码:

html head base href=' %=base path % ' 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=' keyword ' content=' keyword 1,keyword2,keyword 3 ' meta http-equiv=' description ' content='这是我的页面'!- link rel='样式表' type=' text/CSS ' href=' styles . CSS ' rel=' external no follow ' rel=' external no follow '-/head body!-获取当前web项目的名称-% string pa=request . getcontextpath();% !-form form page jump-form action=' result . JSP ' method=' post ' input type=' radio ' name=' option ' value=' jindao ' img alt='剪刀' src=' http :%=pa %/images/jindao . jpg ' input type=' radio ' name=' option ' value=' shitou ' img alt=stone ' src=' http 3360%=pa %/images/shitou . jpg ' input type=' radio ' name=' option ' value='

游戏结果页面代码:

html head base href=' %=基路径% ' title my JSP '结果。“JSP”起始页/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=' keyword ' content=' keyword 1,keyword2,keyword 3 ' meta http-equiv=' description ' content='这是我的页面!- link rel='样式表type=' text/CSS ' href=' style。CSS ' rel='外部无跟随' rel='外部无跟随'-/头体% String路径new=请求。getcontextpath();//获取游戏参与者的选项值字符串RES=请求。getParameter(' option ');字符串reslong=res ' .jpg ';//产生随机数进行匹配图片int com=(int)Math。random()* 3;字符串计算机=字符串。value of(com);//计算机=0金岛//计算机=1尤氏//计算机=2 bu if(计算机。equals(' 0 '){ computer=' jindao ';} else if(计算机。equals(' 1 '){ computer=' shitou ';} else { computer=' bu } String computer long=computer ' .jpg ';int win=0;int lost=0;int ping=0;对象obj win=会话。GetAttribute(' win ');object objlost=session。GetAttribute(' lost ');对象对象ping=会话。GetAttribute(' ping ');if(objwin==null){ session。setattribute(' win ',字符串。(win)的值;} if(objlost==null){ session。setattribute('丢失',字符串。(损失的)价值;} if(obj ping==null){ session。setAttribute(' ping ',String。值(ping));} % h3结果是/h3!-图片传值-您出的是:img alt=' src=' http :%=路径new %/images/%=reslong % ' span style=' bold ' color=' red ' vs/span电脑出的是:img alt=' src=' http :%=路径new %/images/%=计算机long % ' %//逻辑判断,会话更新统计值,if(RES .等于(计算机)){ out。println('平局!');//session.setAttribute('ping ',String。(整数)的值。((字符串)会话的值。GetAttribute(“ping”)1));session.setAttribute('ping '),字符串。(整数)的值。((字符串)会话的值。getattribute(“ping”)1));} else if((RES . equals(' jiandao ')计算机。equals(' bu ')|(RES . equals(' shitou ')计算机。equals(' jiandao ')| |(RES . equals(' bu ')计算机。等于('尤氏))){ out.println('您赢了!');//session.setAttribute('win ',String。(整数)的值。((字符串)会话的值。getattribute(' win ')1));session.setAttribute('win ',String。(整数)的值。((字符串)会话的值。getattribute(' win ')1));} else{ out.println('您输了!');session.setAttribute('lost ',String。(整数)的值。((字符串)会话的值。getattribute(' lost ')1));//session.setAttribute('lost ',String。(整数)的值。((字符串)会话的值。getattribute(' lost ')1));} % h3统计结果,待写入数据库/h3您一共玩了%=字符串。(整数)的值。((字符串)会话的值。getattribute(' win ')整数。((字符串)会话的值。getattribute(' lost ')整数。((字符串)会话的值。getattribute(' ping '))%局br/您赢了%=字符串。(整数)的值。((字符串)会话的值。GetAttribute(' win ')%局br/您输了%=字符串。(整数)的值。((字符串)会话的值。getattribute(' lost ')%局br/打平了%=字符串。(整数)的值。()的值字符串)会话。GetAttribute(' ping ')%局br//body/html游戏结果:

这个绝对没作弊,因为我输了5局哪!老师还提过下次要写个类似老虎机的游戏,估计在这个程序上做作弊就可以了。

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

更多资讯
游戏推荐
更多+