javascript SyntaxError:意外的令牌案例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24463293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 02:50:42  来源:igfitidea点击:

SyntaxError: Unexpected token case?

javascriptswitch-statement

提问by user3785160

There are five cases that are accepted. Still kind of a newbie to Java, so this is a slightly odd case. As far as I can tell (I've read the code at least twice) it should work perfectly fine, unless the double switch is making it not function... I get the error "SyntaxError: Unexpected token case"

有五种情况被接受。仍然是 Java 的新手,所以这是一个有点奇怪的案例。据我所知(我至少阅读了两次代码)它应该可以正常工作,除非双开关使它不起作用......我收到错误“SyntaxError:意外的令牌案例”

var shouldWeapon = String("sword");
var user = prompt("There's a duck in a pond. It likes fish. What do you do? Would you like to feed it, kill it, skin it, buy it, or fight it").toLowerCase();

switch(user) {
    case 'feed it': 
        var whatHaveFood = prompt("What do you have for food?").toLowerCase();
            switch(whatHaveFood) {
                case 'pancakes':
                    console.log("Great! Ducks love their pancakes!");
                    break;
                case 'muffins':
                    console.log("I'm sorry what? You carry muffins? Ducks LOOOOOOOOOOOVE MUFFINS LIKE OMIGOSH I LOVE MUFFINS MMMM M M MMMM MMM IN MY TUMMY.");
                    break;
                case 'dormant spiders':
                    console.log("You decide not to give them to the duck. They're yours. Nobody gets your dormant spiders.");
                    break;
                case 'apple':
                    console.log("OH BOY I LOVE APPLES -said no duck ever.");
                    break;
                default:
                    console.log("The Duck doesn't like that. He curses you to the pits of hell and walks away.");
                    break;
            };
        break;
    case 'kill it':
            var whatHaveWeapon = prompt("What sort of weapon do you have?").toLowerCase();
            if(shouldWeapon || whatHaveWeapon){
                console.log("Why Aren't you using a sword? Why are you using a " + String(whatHaveWeapon) + ". They Suck!");
                }else{
                    console.log("Good choice. The Duck is vanquished.");
                }
        break;
    case 'skin it':
            var tempCat = prompt("What temperature is the cat?");
            if(tempcat > 4){
                console.log("Don't skin ducks.");
            }
            else{
                console.log("That's a freaking cold cat.");
            }
        break;
    case 'buy it':
            var buyDuckCost = Math.floor(Math.random()*5 + 1);
            var buyDucky = ("How much money do you have?");
            var missingMoney = buyDuckCost - buyDucky;
            if(buyDucky >= buyDuckCost){
                console.log("You have bought a duck! congratulations!");
            }
            else{
                console.log("I'm sorry you don't have that much money. You still need" + String(missingMoney) + "$! The duck pulls out a gun and shoots you.");
        break;
    case 'fight it':
        var Smickle = true
        var Donkey = false
        if(Donkey || Smickle){
            console.log("YOU CAN'T FIGHT THE DUCK. THE DUCK IS TOO STRONG");
        }
        else{
            console.log("Ummmm... this is the only accessible answer..... OMEGA GOOD JOB*Cute anime loli voice.*")
        }
        break;
    console.log("What? You're going to do what with the duck?")
    default:

}

As far as I know, this should function....

据我所知,这应该起作用......

回答by Ed Morales

In this part ("buy it" case), you are missing this end brace.

在这部分(“购买”案例)中,您缺少这个末端支撑。

else {
  console.log("I'm sorry you don't have that much money. You still need" + String(missingMoney) + "$! The duck pulls out a gun and shoots you.");
} //<<-- missing this end brace
break;

Code working here

代码在这里工作