If, If else语句产生错误消息

if, if else statement producin error message

本文关键字:If 错误 消息 语句 else      更新时间:2023-09-26

我理解每个if语句和每个if else语句都包含在花括号内。这就是我目前所做的。我刚开始编写这段代码,并且只测试了20%,这样我就可以在代码变得太长之前修复错误。一切看起来都是正确的,我得到了一个语法错误,对于我的生活,我无法看到它在哪里。有什么建议吗?下面是代码。链接在这里:stickFigure

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>The mysterious road</title>
</head>
<script type="text/javascript">
var curScene = 0
function changedScene(decision){
var message = "";
if(curScene==0) {
curScene=1;
message = "Let the games began!";
}
else if(curScene ==1){
if(decision==1){
    curScene = 2;
    message = "Looks like you're on the right road.";
}
else(curScene = 3);
message = "you're stading on a bridge overlooking a peaceful stream.";
}
document.getElementById("sceneimg"). src = "scene" + curScene + .png; //There's a syntax  error       here that I don't see!
alert(message);
}
</script>
<body>
<div style="margin-top:100px; text-align:center">
<p><img id="sceneimg" src="../sfa/scene0.png" alt="Stick Figure" /></p>
Enter here for a glorious adventure!
<input type="button" id="decision" value="1" onclick="curScene(1)" />
Enter this gate for the surpirse of your life!
<input type="button" id="decision" value="2" oonclick="curScene(2)" />
</div>
</body>
</html>

看这里

else(curScene = 3);

你应该能算出来

对于第一个语法错误,您要注意在".png"

周围添加双引号

并且您没有得到支持调用curScene(1)的方法(没有找到函数)的ant对象,并且您唯一的函数是changedScene()加上oonclick的错字。和fearofawhackplanet说的话

坚持下去,不要放弃

另外,即使JavaScript允许,您在第一个脚本语句之后也缺少分号:

var curScene = 0

要注意这样的遗漏。尽管这里不是语法或语义错误,但这可能会给您在其他地方带来麻烦,例如(made up):

return  // return undefined
val1 + val2;

因为在其他地方都使用分号来结束语句,所以最好保持一致。