简体   繁体   English

JavaScript中的Else if和Else

[英]Else if and Else in JavaScript

I wrote a small javascript code, but it has a problem, the page can not show anything. 我写了一个小的javascript代码,但是有一个问题,该页面无法显示任何内容。 I think the problem is in the line "else if..." or "else...", because if I comment these two lines, the code runs without any problem 我认为问题出在“ else if ...”或“ else ...”行中,因为如果我注释这两行,则代码可以正常运行

<html>
<head>
    <script language="javascript">
        var var1 = window.prompt("please input");
        var var2 = window.prompt("please input2");
        var1 = parseFloat(var1);
        var2 = parseFloat(var2);

        if (var1< var2) {document.writeln("the second number is bigger")};
        else if (var1> var2) {document.writeln("the first number is bigger")};
        else {document.writeln("They are the same")};
    </script>
</head>
<body>
</body>
</html>

Your javascript should be like this 您的JavaScript应该是这样的

 var var1 = window.prompt("please input"); var var2 = window.prompt("please input2"); var1 = parseFloat(var1); var2 = parseFloat(var2); if (var1 < var2) { document.writeln("the second number is bigger"); } else if (var1 > var2) { document.writeln("the first number is bigger"); } else { document.writeln("They are the same"); } 

Should be: 应该:

<html>
<head>
    <script language="javascript">
        var var1 = window.prompt("please input");
        var var2 = window.prompt("please input2");
        var1 = parseFloat(var1);
        var2 = parseFloat(var2);

        if (var1 < var2) {
            document.writeln("the second number is bigger");
        }
        else if (var1 > var2) {
            document.writeln("the first number is bigger");
        }
        else {
            document.writeln("They are the same");
        }
    </script>
</head>
<body>
</body>
</html>

your semi colons were wrong 你的半冒号错了

Try removing the semicolon, ; 尝试删除分号; , from after the brackets of your if statements : ,从if statements的括号后面开始:

    if (var1< var2) {document.writeln("the second number is bigger")}
    else if (var1> var2) {document.writeln("the first number is bigger")}
    else {document.writeln("They are the same")}

Have a look at this SO answer: https://stackoverflow.com/a/17036218/4206206 看看这个SO答案: https : //stackoverflow.com/a/17036218/4206206

Essestially, a semicolon isn't used to end a group of statements, but rather to end a single statement. Essestially,分号不使用结束一组语句,而是结束一个语句。


A point with your code, if you're using HTML5 you don't need the language="javascript" in your script tags: 代码要点,如果您使用的是HTML5,则不需要在脚本标签中使用language="javascript"

<script language="javascript">

Can become simply 可以变得简单

<script>
if (var1< var2) {document.writeln(" ... ")};
                                           ^

Your semicolons at the end of your if, else if, and else blocks are your issue. 您在if,else if和else块末尾的semicolons是您的问题。 They are prematurely ending your if blocks. 他们过早地结束了if块。

Just make sure you parseFloat before you compare, because it just compares as string, and remember, 10 comes before 2, so by string 10 < 2 ! 只需在比较之前确保您parseFloat ,因为它只是作为字符串进行比较,并且请记住,10在2之前,所以按字符串10 < 2 And you don't need the ; 而且您不需要; at the end: 在末尾:

var var1 = window.prompt("please input");
var var2 = window.prompt("please input2");
var1 = parseFloat(var1);
var2 = parseFloat(var2);

if (var1 < var2) {
  document.writeln("the second number is bigger");
} else if (var1 > var2) {
  document.writeln("the first number is bigger");
} else {
  document.writeln("They are the same");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM