Javascript else-if语句错误
Javascript else if statement error
我必须制作一个满足三角形不等式定理的javascript代码,两个较小的边加起来比最大的边大。我必须使用javascript并使用提示用户输入三个数字。我不能要求用户输入我必须通过代码找到的最大数字。下面是我到目前为止得到的结果,但我在第一个else-if语句中一直出现错误,所以我不会运行。知道我的代码出了什么问题吗?
<script type="text/javascript">
<!--
var a = prompt("Enter the first side", "0");
a = Number(a);
var b = prompt("Enter the second side", "0");
b = Number(b);
var c = prompt("Enter the third side", "0");
c = Number(c);
if(a>=b, a>=c){
if (b+c>a) {
document.write("These numbers " + a + ", " + b + ", and " + c + " do satisfy the triangle inequality.1" );
}
else {
document.write("These numbers " + a + ", " + b + ", and " + c + " do not satisfy the triangle inequality.1" );
}
else if(b>=c, b>=a) {
if (c+a>b) {
document.write("These numbers " + a + ", " + b + ", and " + c + " do satisfy the triangle inequality.2" );
}
else {
document.write("These numbers " + a + ", " + b + ", and " + c + " do not satisfy the triangle inequality.2" );
}
}
else {
if (a+b>c) {
document.write("These numbers " + a + ", " + b + ", and " + c + " do satisfy the triangle inequality.3" );
}
else {
document.write("These numbers " + a + ", " + b + ", and " + c + " do not satisfy the triangle inequality.3" );
}
}
}
// -->
</script>
if
语句测试单个条件,而不是多个条件。假设您想知道两个a
都大于或等于b
并且a
大于或等于c
,则需要使用&&
运算符:
if(a>=b && a>=c)
如果是OR,则是||
运算符:
if(a>=b || a>=c)
没有这样的东西
if(a>=b, a>=c){
如果你想检查两者是否为真,请使用:
if (a >= b && a >= c)
如果您想检查或:
if (a > = || a >= c)
但总的来说,这个代码看起来非常糟糕。
if ((a >= b) && (a >= c))
在谷歌上搜索"javascript if"或类似的内容来了解更多信息。
我还注意到你的问题与你的答案非常不同。你说你必须求两边,然后计算第三方,但在你的回答中,你问了所有三方,然后通知用户他们是否正确。。。
是否必须使用if语句。
var input2 = [10, 30, 50, 150];
input2.sort(function(a, b){return b-a});
console.log(input2[0]);
你可以在firefox的firebug控制台中运行这个,看看它能做什么。更正了我的答案。
谢谢大家,我知道我的代码很草率,但我才刚刚开始,所以我会及时变得更好。这是我修复代码的方法。
<script type="text/javascript">
<!--
var a = prompt("Enter the first side", "0");
a = Number(a);
var b = prompt("Enter the second side", "0");
b = Number(b);
var c = prompt("Enter the third side", "0");
c = Number(c);
if(a>=b && a>=c && b+c>a){
document.write("These numbers " + a + ", " + b + ", and " + c + " do satisfy the triangle inequality." );
} else if(b>=c && b>=a && c+a>b) {
document.write("These numbers " + a + ", " + b + ", and " + c + " do satisfy the triangle inequality." );
} else if(c>=a && c>=b && a+b>c) {
document.write("These numbers " + a + ", " + b + ", and " + c + " do satisfy the triangle inequality." );
} else {
document.write("These numbers " + a + ", " + b + ", and " + c + " do not satisfy the triangle inequality." );
}
// -->
</script>
相关文章:
- 将错误与if语句混淆
- 如何知道javascript for语句中的所有结果是否都是错误的
- MVC Jquery-按钮点击-获取if语句错误
- 由 if / else 语句中的无效语法导致的语法错误
- Javascript else-if语句错误
- 阻止表单提交 javascript 为什么有一个明显的错误语句返回 true
- 筛选器后的行计数错误(使用if语句)
- Simple Javascript if语句中存在语法错误
- Null记录错误地触发Else语句
- Javascript if else语句OR条件错误
- 如果语句返回错误答案
- 防止 if 语句中的错误“未定义”
- 使用 If else if else 语句得到错误
- ES6 大箭头符号函数给出语法错误:缺失;在语句之前
- 非法返回语句错误
- 为什么我的 if 语句出现语法错误
- 为什么以下语句执行没有错误
- 我的“if”语句中包含中断的错误(wrox ch 3 练习 4)
- 为什么在调用丢失的函数时会捕获错误,而不是在错误语句中捕获错误
- jQuery无线电检查命中错误语句