【问题标题】:expected an identifier but instead found 'else' [closed]期望一个标识符,但发现'else' [关闭]
【发布时间】:2015-01-25 05:07:53
【问题描述】:

您好,我正在尝试制作“选择您自己的冒险”游戏,但出现此错误。我似乎无法弄清楚如何解决这个问题,任何帮助将不胜感激 这是我到目前为止的编码,

var age = prompt("how old are you?");

if (age > 10) {
    alert("you may proceed");
} else; {
    alert("I think you should leave, NOW");
}

alert(
    "you are in a small room sitting in a desk. there is a door right        behind you PLEASE NOTE the apocalypses has just started there are only a few people in your building alive think about finding a weapon"
);
var userawnser = prompt(
    "Do you 'type 1 to' examine the room or '2' exit through the door?"
);
if (userawnser = 1) {
    alert(
        "You see a stapler on your desk a bat by the door and a computer.You grab the bat"
    );
} else if (userawnser = 2); {
    alert(
        "you walk outside of you room and are surprise attacked by a zombie"
    );
    alert(
        "you attempt to get the zombie off but since it got you by surprise you are bitten in the back of the neck and join the undead army"
    );
    confirm(
        "never go places unprotected silly or your guts will be harvested again!"
    );
} else; {
    alert("that doesn't make any since!");
}

【问题讨论】:

  • 您有一个错字(else 之后的额外;)。你的代码还有很多很多其他问题,比如使用=而不是==(这会破坏整个事情),严重缺乏缩进,任意空格等。我建议你去找一个好的@ 987654321@.
  • 在测试相等性时使用==,而不是=
  • 不要将; 放在elseif 语句中的测试子句之后。
  • "that doesn't make any since!" - 是的,没错。

标签: javascript debugging


【解决方案1】:

最后一个 else 后不应有分号。

考虑答案的顺序。假设您希望用户在userawnser == 1(又名true)时被直接发送到游戏。如果userawnser == 2(又名“用户退出门”),那么代码应该通知用户他们不能玩。

在下面试试我的代码 sn-p:

var age = prompt("how old are you?");
var userawnser;
//----------------------------------------------
//answer >=10
if (age >= 10) {
    alert("you may proceed");
    alert("you are in a small room sitting in a desk. there is a door right behind you PLEASE NOTE the apocalypses has just started there are only a few people in your building alive think about finding a weapon");

    userawnser = prompt("Do you 'type 1 to' examine the room or '2' exit through the door?");

    if (userawnser == 1) {
        alert("You see a stapler on your desk a bat by the do`enter code here`or and a computer.You grab the bat");
    } else if (userawnser == 2) {
        alert("you walk outside of you room and are surprise attacked by a zombie");

        alert("you attempt to get the zombie off but since it got you by surprise you are bitten in the back of the neck and join the undead army");
    } else {
        alert("Sorry, what number you choice?");
    }

    var confirmNeverGo = confirm("never go places unprotected silly or your guts will be harvested again!");
    if (confirmNeverGo === false) {
        alert("that doesn't make any since!");
    }

//------------------------------------------------------------------
//answer <=10(poor kid)
} else {
    alert("I think you should leave, NOW");
}

我鼓励您在论坛上做更多的研究,以便您了解如何做事。致力于清理你的代码。如果一开始不起作用,请不要气馁。

【讨论】:

    猜你喜欢
    • 2022-08-16
    • 2020-09-25
    • 2016-12-31
    • 2018-05-09
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多