我正在用 javascript 编写一个简单的三角函数程序,而我的 if 和 while 语句不能正常工作,因为它们只有在第一个条件为真时才会通过,即如果你输入 Sine 它将工作,但如果你输入 Cosine 或 Tangent 则不会。
<script language="JavaScript">
var opposite = 1
var adjacent = 1
var hypotenuse = 1
var sct = "SohCahToa"
while (!(sct == ("Sine" || "Cosine" || "Tangent"))) {
sct = prompt("Sine (unknown adjacent) / Cosine (unkown opposite side) / Tangent (unknown hypotenuse)")
if (!(sct == ("Sine" || "Cosine" || "Tangent"))) {
alert("Spelling error, please try again")
}
}
if (sct == ("Sine" || "Cosine"))
hypotenuse = prompt("What is the hypotenuse")
if (sct == ("Sine" || "Tangent"))
opposite = prompt("What is the opposite side")
if (sct == ("Tangent" || "Cosine"))
adjacent = prompt("What is the adjacent side")
谢谢(将代码另存为 .html 以进行测试)