JavaWeb(猜拳游戏)

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>猜拳游戏</title>
        
        <style type="text/css">
            
            #display1{
                float: left;
            }
            
            #display2{
                float: right;
                margin-right: 302px;
            }
            
            #display3{
                float: right;
                margin-top: -200px;
            }
            
            #wanjiadisplay1{
                float: left;
            }
            
            #wanjiadisplay2{
                float: right;
                margin-right: 302px;
            }
            
            #wanjiadisplay3{
                float: right;
                margin-top: -200px;
            }
            
            #wanjiadisplay31{
                
                margin-left: 80px;
                margin-top: 50px;
            }
            
            
        </style>
        
    </head>
    <body>
        
        <div id="d" style="height: 550px; width: 700px; border: solid 1px; background-color: brown;">
            
            <div id="display" style="height: 200px ;width: 700px; border: solid 1px;color: white;">
                <div id="display1" style="height: 198px; width: 298px; border: solid 1px;">
                    <div style="margin-left: 120px;">你</div>
                    <div id="i2" style="margin-left: 100px; margin-top: 50px;"></div>
                </div>
                <div id="display2" style="height: 198px;width: 96px; border: solid 1px;">
                    <div style="font-size: 50px; color: black; margin-top: 80px; margin-left: 25px;">VS</div>        
                </div>
                <div id="display3" style="height: 198px;width: 300px; border: solid 1px; ">
                    
                    <div style="margin-left: 120px;">电脑</div>
                    <div id="i3" style="margin-left: 100px; margin-top: 50px;"></div>
                    
                </div>
            </div>
            
            
            <div  style="width: 698px; height: 100px; border: solid 1px;color: white;">
                
                <div id="result" style="margin-left: 300px; margin-top: 40px;">你赢了</div>
                
            </div>
            
            <div id="wanjiadisplay" style="width: 700px; height: 200px; border: solid 1px; color: white;">
                
                <div id="wanjiadisplay1" style="height: 198px; width: 298px; border: solid 1px;">
<!--                     <img src="" style="margin-top: 80px; margin-left: 100px;" id="i" />    -->
                
    

                <div id="i" style="margin-left: 100px; margin-top: 70px;">石头</div>
    
                </div>
                
                <div id="wanjiadisplay2" style="height: 198px;width: 96px; border: solid 1px;">
                    
                </div>
                <div id="wanjiadisplay3" style="height: 198px;width: 300px; border: solid 1px; ">
                    
                    <div id="wanjiadisplay31" >
                        请选择出拳:
                        <span>
                            <select name="sel" id="sel" οnchange="show()">
                                <option value=""></option>
                                <option value="石头">石头</option>
                                <option value="剪刀">剪刀</option>
                                <option value="布">布</option>
                            </select>
                        </span>
                        
                        <button type="button" id="selshow" style="margin-top: 10px;" οnclick="selshow1()" >选好了,出拳</button>
                        
                        <button type="button" id="selshow()" οnclick="selshow2()" >重新选择</button>
                    </div>
                </div>
            </div>
            
            
            <div id="showresult"  style="width: 700px;height: 50px; border: solid 1px;color: white;">
                <div style="margin-left: 250px;">
                总局数:<span id="zongjushu"></span>赢:<span id="ying"></span>输:<span id="shu"></span>
                <div/>
            </div>
            

        </div>
        
        
    </body>
    
    <script type="text/javascript">
        
        var count=0;
        var scount=0;
        var fcount=0;
        
        function autoshow(){
            
            var i3=document.getElementById("i3");
            
            
            var num=Math.random()*100;
            num=Math.ceil(num);
            num=num%3;
            switch(num){
                
                case 0:
                i3.innerHTML="石头";
                break;
                case 1:
                i3.innerHTML="剪刀";
                break;
                case 2:
                i3.innerHTML="布";
                break;
                
            }
        }
        
        function selshow1(){
            
            count++;
            var s=document.getElementById("sel");
            var choice=s.value;   
            var i=document.getElementById("i");
            var i2=document.getElementById("i2");
            var display=document.getElementById("result");
            
            if(choice=="石头"){
                i.innerHTML="石头";
                i2.innerHTML="石头";
            }else if(choice=="剪刀"){
                i.innerHTML="剪刀";
                i2.innerHTML="剪刀";
            }else if(choice=="布"){
                i2.innerHTML="布";
                i.innerHTML="布";
            }
            
            //调用autoshow()函数,电脑随机出拳
            autoshow();
            var i3=document.getElementById("i3");
            
            if(choice=="石头"&&i3.innerHTML=="剪刀"){
                scount++;
                display.innerHTML="你赢了";
            }else if(choice=="剪刀"&&i3.innerHTML=="布"){
                scount++;
                display.innerHTML="你赢了";

            }else if(choice=="布"&&i3.innerHTML=="石头"){
                scount++;
                display.innerHTML="你赢了";

            }else if(choice==(i3.innerHTML)){
                display.innerHTML="平局";
            }else {
                fcount++;
                display.innerHTML="电脑赢了";
            }
            
            var zjs=document.getElementById("zongjushu");
            zjs.innerHTML=count;
            var y=document.getElementById("ying");
            y.innerHTML=scount;
            var s=document.getElementById("shu");
            s.innerHTML=fcount;
            
            
        }
        
        
        
        function selshow2(){
            
            var s=document.getElementById("sel");
            s.value="";
            
        }
        
    
    </script>

</html>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单JavaWeb猜拳游戏实现的代码: Java代码(JSP页面): ```jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>猜拳游戏</title> </head> <body> <h2>猜拳游戏</h2> <form action="play.jsp" method="post"> <input type="radio" name="player" value="1" checked> 石头 <input type="radio" name="player" value="2"> 剪刀 <input type="radio" name="player" value="3"> 布 <br><br> <input type="submit" value="提交"> </form> </body> </html> ``` Java代码(play.jsp页面): ```jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>猜拳游戏</title> </head> <body> <% int player = Integer.parseInt(request.getParameter("player")); // 获取用户选择 int computer = (int) (Math.random() * 3) + 1; // 电脑随机出拳 int result = (player - computer + 3) % 3; // 计算胜负 %> <h2>猜拳游戏</h2> <p>你出拳: <% switch (player) { case 1: out.print("石头"); break; case 2: out.print("剪刀"); break; case 3: out.print("布"); break; } %> </p> <p>电脑出拳: <% switch (computer) { case 1: out.print("石头"); break; case 2: out.print("剪刀"); break; case 3: out.print("布"); break; } %> </p> <p> <% switch (result) { case 0: out.print("平局!"); break; case 1: out.print("你赢了!"); break; case 2: out.print("你输了!"); break; } %> </p> </body> </html> ``` 此代码实现了一个简单猜拳游戏,用户可以在页面上选择石头、剪刀、布中的一种,点击“提交”按钮后,对应的Java代码会生成一个随机数作为电脑出拳,然后计算出胜负并在页面上展示出来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值