网络编程 
首页 > 网络编程 > 浏览文章

jsp实现剪子石头布小游戏

(编辑:jimmy 日期: 2024/5/9 浏览:3 次 )

本文实例为大家分享了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="/UploadFiles/2021-04-02/jiandao.jpg">

游戏界面:

jsp实现剪子石头布小游戏

游戏结果页面代码:

<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="/UploadFiles/2021-04-02/<%=reslong %>">

游戏结果:

jsp实现剪子石头布小游戏

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

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

上一篇:JSP实现简单人事管理系统
下一篇:详解JSP 内置对象request常见用法
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。