js——红黑球计算

# JJU_干干 

 

代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>

    <!-- 球函数 -->
    <script type="text/javascript">
        function Ball(){
            var  rw = prompt("请输入红白球个数:"); //输入
            rw = parseInt(rw) && Number(rw)
            var  wb = prompt("请输入白黑球个数:");
            wb = parseInt(wb) && Number(wb)
            var  rb = prompt("请输入红黑球个数:");
            rb = parseInt(rb) && Number(rb);
            if(!isNaN(rw)&&!isNaN(wb)&&!isNaN(rb)){
                var b = (wb-rw+rb)/2;
                var w = wb-b;
                var r = rw-w;
                var ball = [rw,wb,rb,w,r,b];
                return ball
            }else{
                alert("输入有误!")
            }

        }
    </script>

</head>
<body>

    <div>
        <!-- 三个输入框,通过 js 代码 显示 value属性-->
        <p>
            红白球共:<input type="text" id="rw"> 
        </p>
        <p>
            白黑球共:<input type="text" id="wb"> 
        </p>
        <p>
            红黑球共:<input type="text" id="rb"> 
        </p>
        <p>
            白:<input type="text" id="w"> 
        </p>
        <p>
            红:<input type="text" id="r"> 
        </p>
        <p>
            黑:<input type="text" id="b"> 
        </p>
    </div>

    <div>
        <script type="text/javascript">
            // 调用Yuan()方法:输入半径,返回半径、周长和面积组成的数组
            var ball = Ball();
            document.getElementById('rw').value = ball[0];  
            document.getElementById('wb').value = ball[1];
            document.getElementById('rb').value = ball[2];
            document.getElementById('w').value = ball[3];  
            document.getElementById('r').value = ball[4];
            document.getElementById('b').value = ball[5];
        </script>
    </div>

</body>
</html>

 

运行结果:

 

 

posted @ 2020-11-30 11:46  ZengZG  Views(411)  Comments(0Edit  收藏  举报