Codecademy:杀死你自己的龙|调试帮助 [英] Codecademy: Slay your own dragon | debugging help

查看:82
本文介绍了Codecademy:杀死你自己的龙|调试帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个杀死你自己的龙游戏,这很简单,只需看一下我即将展示的代码就会让你知道它是什么。无论如何,我编程时得到了SyntaxError:Unexpected token else。我试图解决它,但我不知道我哪里出错:(。也许你们可以帮助我。

继承人代码:



I was coding a "slay your own dragon game," it is quite simple, just by looking at the code i'm about to show you will give you an idea of what it is about. Anyways, I got "SyntaxError: Unexpected token else" as I was programming. I tried to solve it, but I didn't know where I went wrong :(. Maybe you guys can help me.
Heres the code:

var slaying = true
var youHit = Math.floor(Math.random() * 2)
var damageThisRound = Math.floor(Math.random()*5 + 1)
var totalDamage = 0

while(slaying) {
    if(youHit) {
        console.log("You hit the Dragon!");
        totalDamage += damageThisRound;
        if(totalDamage >= 4) {
            console.log("You slew the Dragon");
            slaying = false
    } else {
        youHit = Math.floor(Math.random() * 2)
    }

    else {
        console.log("The dragon defeated you");
        slaying = false;
    }
}





我不是最好的编码员(显然),所以你可以做你的回答简单只适合我?谢谢!感谢您的帮助!



I'm am not the best coder(obviously), so can you make your answer simple just for me? Thanks! I appreciate your help!

推荐答案

使用正确的缩进格式化代码可以帮助您很多...

请看第一个 - 它属于内部if,但没有关闭}之后,所以第二个是无配对...

Formatting your code with the right indentation can help you a lot...
See the first else - it belongs the the inner if, but there is no closing } after it so the second else is pair-less...
var slaying = true
var youHit = Math.floor(Math.random() * 2)
var damageThisRound = Math.floor(Math.random()*5 + 1)
var totalDamage = 0

while(slaying) {
    if(youHit) {
        console.log("You hit the Dragon!");
        totalDamage += damageThisRound;
        if(totalDamage >= 4) {
            console.log("You slew the Dragon");
            slaying = false
        } else {
            youHit = Math.floor(Math.random() * 2)
        }
    // here you missign a closing }
    else {
        console.log("The dragon defeated you");
        slaying = false;
    }
}


很高兴看到你发现了这个问题。如果您的开发环境指向特定的代码行,通常很容易找到语法错​​误。如果您使用的是Visual Studio,则可以使用菜单命令格式化文档,这有时会告诉您哪行是错误的,但只有当所有语法都正确时,它才会格式化文档。



在Visual Studio中,你通常会在错误的语法部分下面得到一条红色的波浪线。
Glad to see you found the issue. Syntax errors usually are pretty easy to find if your development environment points to a specific line of code. If you are using Visual Studio you can use the menu command "Format Document" and that will sometimes tell you which line is wrong but it will also only format the document when all the syntax is correct.

In Visual Studio, you'll get a red squiggly line usually under the bad syntax part as well.


是的,我认为这段代码是正确的方法,只需再添加一个}



查看:

Yeah, I think this code is the proper way, just need to add one more "}"

Check it out:
var slaying = true
var youHit = Math.floor(Math.random() * 2)
var damageThisRound = Math.floor(Math.random()*5 + 1)
var totalDamage = 0

while(slaying) {
    if(youHit) {
        console.log("You hit the Dragon for" + " " + totalDamage);
        totalDamage + damageThisRound;

        if(totalDamage >= 4) {
            console.log("You slew the dragon!");
            slaying = false
        } else {
            youHit = Math.floor(Math.random() * 2)
        }
    } else {
        console.log("The dragon defeated you");
        slaying = false
    };

}


这篇关于Codecademy:杀死你自己的龙|调试帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆