function calcPyth() {
let v1 = document.getElementById("v1").value;
let v2 = document.getElementById("v2").value;
let v3 = document.getElementById("v3").value;
if ((v1 * v1) == (v2*v2)+(v3*v3)) {
alert("These are pythagoras triplets");
} else if ((v1 * v1) < (v2*v2) + (v3*v3)) {
alert("This is an obtuse triangle");
} else {
alert("This is an acute triangle");
}
}
当我尝试运行该语句时,它仅返回else if值。我不知道该怎么做。
答案 0 :(得分:0)
您需要检查毕达哥拉斯三联体的所有可能组合。
function calcPyth() {
let v1 = parseInt(document.getElementById("v1").value);
let v2 = parseInt(document.getElementById("v2").value);
let v3 = parseInt(document.getElementById("v3").value);
if ((v1 * v1) == (v2*v2)+(v3*v3) || (v2 * v2) == (v1*v1)+(v3*v3) || (v3 * v3) == (v1*v1)+(v2*v2)) {
alert("These are pythagoras triplets");
} else if ((v1 * v1) < (v2*v2) + (v3*v3)) {
alert("This is an obtuse triangle");
} else {
alert("This is an acute triangle");
}
}
<input type="text" id="v1" value="3"/><br/>
<input type="text" id="v2" value="4"/><br/>
<input type="text" id="v3" value="5"/><br/>
<button onClick="calcPyth()">calcPyth</button>