【问题标题】:Javascript - Calculating square root of an cylinderJavascript - 计算圆柱的平方根
【发布时间】:2017-09-30 11:07:37
【问题描述】:

我想编写一个计算圆柱平方根的程序。当我在其他地方计算时,结果太高了。

<html>
<head>
<title>Example</title>
<script type="text/javascript">
function calculate(){
    var d = document.getElementById('d')
    var v = document.getElementById('v');
    var height = d.value;
    var diameter = v.value;
    var result = (Math.PI * (height * height) * (diameter / 100));
    console.log (result);
}
</script>
</head>
<body>
<h4>Height<h4>
<input id = 'd'></input>
<h4>Diameter<h4>
<input id = 'v'></input>
<button  onClick "calculate()" type = "submit">Calculate</button>
</body>
</html>

【问题讨论】:

  • 我认为您的意思是“音量”,即pi*radius^2*height,即。 Math.PI * diameter/2 * diameter/2 * height。您的代码目前正在尝试pi*height^2*diameter/100,这没有任何意义。
  • 为什么在计算体积时使用名为“d”的变量表示高度,使用名为“v”的变量表示直径 - h、d 和 v(而不是模棱两可的“结果”)分别不会更容易混淆吗?
  • square root of a cylinder ...首先你需要切线的斜边乘以导数的角度

标签: javascript square-root


【解决方案1】:

更简单:

<html>
    <head>
    <title>Example</title>
    <script type="text/javascript">
    function calculate(){
        var d = document.getElementById('d').value,
            /* calc radius */
            r = (d / 2),
            /* calc area */
            A = (Math.PI * Math.pow(r, 2));
        console.log (A);
    }
    </script>
    </head>
    <body>
        <h4>Diameter<h4>
        <!-- input is selfclosing: -->
        <input id="d" />
        <!-- don’t forget the equal sign after onclick -->
        <button onclick="calculate()" type="button">Calculate</button>
    </body>
</html>

【讨论】:

    猜你喜欢
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    • 2017-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多