JSP实现石头剪刀布游戏

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<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">
	-->

  </head>
  
  <body>
      <%  
     
    String pathnew=request.getContextPath();  
    //获取游戏参与者的选项值  
    String res=request.getParameter("option");  
    String reslong=res+".PNG";  
    //产生随机数进行匹配图片   
    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+".PNG";  
    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 %>/<%=reslong %>">  
        <span style="bold" color="red">vs</span>  
        电脑出的是:<img alt="" src="<%=pathnew %>/<%=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>



index.jsp如下:
<pre name="code" class="plain"><%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.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">
	-->
  </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%>/jiandao.PNG">  
            <input type="radio" name="option" value="shitou"><img alt=石头" src="<%=pa%>/shitou.PNG">  
            <input type="radio"name="option" value="bu"><img alt="布" src="<%=pa%>/bu.PNG">  
            <input type="submit"value="确定"/>  
      </form>  
      </body>
</html>


 


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

终极之旅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值