任何人都可以告诉我的程序有什么问题吗? [英] Can anyone tell whats wrong in my program?

查看:76
本文介绍了任何人都可以告诉我的程序有什么问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一个网页,其中x和y需要2个输入。它应该作为计算器。对于加法和减法我必须使用按钮,这样当我点击它时,需要进行加法和减法计算。对于乘法除法我要使用下拉按钮。当我点击乘法或除法时它应该计算乘法或除法并显示



我尝试过:



i want to do a web page in which it takes 2 inputs for x and y. it should work as calculator. for addition and subtraction i have to use button such that when i click on it addition and subtraction calculation need to be done. for multiplication division i hv to use dropdown button. when i click on multiplication or division it should calculate multiplication or division and display

What I have tried:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC"-//W3C//DTD//DTD XHTML1.1//EN"

"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <h1><title>CALCULATOR</title></h1>
</head>
<body>
    <form>
        <div>
            <label>Value of X : </label>
            <input type="text" id="x" name="x" placeholder="Enter X value"/>
            <div>
            <div>
            <label>Value of Y : </label>
            <input type="text" id="y" name="y" placeholder="Enter Y value"/>
            <div>
            <div><p id="demo"><h1>display here</h1></p></div>
            <div>
            <button onclick="add()">ADDITION</button>&nbsp;&nbsp;&nbsp;
            <button onclick="sub()">SUBTRACT</button>&nbsp;&nbsp;&nbsp;
            <select>
                <option></option>
                <option onclick="mul()">MULTIPLY</option>
                <option onclick="divide()">DIVIDE</option>
            </select>
        </div>
        <script>
        function add(){
            document.getElementById("demo").innerHTML = x + y;
            }
        </script>
        <script>
        function sub(){
            document.getElementById("demo").innerHTML = x - y;
            }
        </script>
        <script>
        function mul(){
            document.getElementById("demo").innerHTML = x * y;
            }
        </script>
        <script>
        function divide(){
            document.getElementById("demo").innerHTML = x / y;
            }
        </script>
    </form>
</body>

推荐答案

你只需要一个将所有函数放入的脚本块,您不需要将每个函数放在自己的脚本块中。还要注意你的开始和结束标签,确保它们匹配并且你的整体html是有效的。



You only need one script block that you put all your functions in, you don't need to put each function in its own script block. Also pay attention to your opening and closing tags, make sure they match and that your overall html is valid.

function add() {
    var xVal = parseInt(document.getElementById('x').value);
    var yVal = parseInt(document.getElementById('y').value);
    document.getElementById("demo").innerHTML = xVal + yVal;
}


这篇关于任何人都可以告诉我的程序有什么问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆