我想编写一个计算圆柱平方根的程序。当我在其他地方计算它时,结果太高了。
<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>
&#13;
答案 0 :(得分:0)
更容易:
<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>