本文介绍了在Javascript中修复关于我的案例的这个语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我一直收到错误SyntaxError:Unexpected token case。我不知道我做错了什么。即使我尝试过,我也无法缩小代码范围。I keep getting the error "SyntaxError: Unexpected token case". I don't know what I am doing wrong. I could not narrow the code down even though I tried.var user = prompt ("Are you a goblin, knight, troll, human, or wizard?").toLowerCase()var name = prompt("What is your name?").toLowerCase()var gender = prompt("Are you male or female?").toLowerCase()switch(user) { case "goblin": console.log("Hello, you are going to have a fun journey as a goblin"); var meetAnt = prompt ("You run into a giant ant eater, you can attack or run. Do you choose to attack?").toLowerCase() if(meetAnt == "yes") { console.log ("you stab him in the eye, but then he hits you with his trunk. You are injured"); var finishAnt = prompt("you can either risk your life and finish him, or try to run away. Do you finish him?").toLowerCase() if(finishAnt == "yes") { console.log("congratulations you have killed the aint eater!") } else if(finishAnt == "no") { console.log("You run away and barley make it, you are badly hurt"); } else { console.log("sorry thats not an option") } } else if(meetAnt == "no") { console.log("You run past the ant eater and get to saftey. You are very lucky") } else { console.log("Sorry that is not an option.") break; case "knight": console.log("Hello, you are going to have a fun journey as a knight"); var dayFight = prompt("You are versing the goblins in a fight, you need to choose how many days you go to fight(1-100). Remember if you choose too little days then you won't have stayed long enough to win the fight but if you stay to long you have more of a chance of dying or getting injured").toLowerCase() if(dayFight = parseInt(dayFight, 10); { console.log("You did not fight enough days, which has resulted in your kingdome loosing the war to the goblins."); } else if(dayFight > 60) { console.log("You have went to war to long and been seriously injured."); } else if(40 <= dayFight && dayFight <= 60) { console.log("You have been at war for the right amount of time. You have came out of the war with no injuries and you also have defieted the goblins!") } break; case "troll"): console.log("Hello, you are going to have a fun journey as a troll"); var red = true; var green = true; var yellow = false; var blue = false; var houseRaid = prompt("You see four huts in the middle of the woods. You can raid two. There is a red hut, blue hut, yellow hut, and green hut. What is one color house that you want to raid?").toLowerCase() var doorPick = prompt("Do you want to enter throuhg the back door or front door?").toLowerCase() if(doorPick||houseRaid == "red"||"green" && "back door") { console.log("You raided these houses and left before any villagers would see you"); } else { console.log("You raided those houses, but one of them was a booby trap and you are now captured"); } break; case "human": console.log("Hello, you are going to have a fun journey as a human"); var reinforceFound = prompt("You know a storm is comming and you have to reinforce your hut, but you only have enough material to reinforce either your lower foundations or higher foundations. Which do you inforce? Higher or lower?").toLowerCase() if(reinforceFound == "lower") { console.log("The storms winds pushed down the top of your house and caved in your roof."); } else if(reinforceFound == "higher") { console.log("The storm did not do that much damage to your house due to your reinforced higher foundations. Good choice"); } else { console.log("sorry but that is not an option. Pick either 'higher', or 'lower'") } break; case "wizard"): console.log("Hello, you are going to have a fun journey as a wizard"); var blood = true; var dust = true; var wings = false; var mushrooms = false; var postion = prompt("You are working on a new healing potion but you do not know what you need to add to finish it. You have 4 ingrediants; blood, dust, wings, mushrooms. Pick one too add. (WARNING: Pick carefully because if you choose the wrong ingerdiant, then your potion will be ruined.)").toLowerCase() if(postion == "wings" || postion == "mushroom") { console.log("You picked a bad ingrediant and now your potion is ruined."); } else if(postion = "dust" || "blood") { console.log("you picked the right ingrediant and your potion is okay") } else { console.log("sorry but that is not an option"); } break; default: console.log("Sorry but that is not a character in the game");};推荐答案var user = prompt ("Are you a goblin, knight, troll, human, or wizard?").toLowerCase();var name = prompt("What is your name?").toLowerCase();var gender = prompt("Are you male or female?").toLowerCase();etc. etc.var user = prompt ("Are you a goblin, knight, troll, human, or wizard?").toLowerCase()var name = prompt("What is your name?").toLowerCase()var gender = prompt("Are you male or female?").toLowerCase()switch(user) { case "goblin": console.log("Hello, you are going to have a fun journey as a goblin"); var meetAnt = prompt ("You run into a giant ant eater, you can attack or run. Do you choose to attack?").toLowerCase() if(meetAnt == "yes") { console.log ("you stab him in the eye, but then he hits you with his trunk. You are injured"); var finishAnt = prompt("you can either risk your life and finish him, or try to run away. Do you finish him?").toLowerCase() if(finishAnt == "yes") { console.log("congratulations you have killed the aint eater!") } else if(finishAnt == "no") { console.log("You run away and barley make it, you are badly hurt"); } else { console.log("sorry thats not an option") } } else if(meetAnt == "no") { console.log("You run past the ant eater and get to saftey. You are very lucky") } else { console.log("Sorry that is not an option.") } break; case "knight": console.log("Hello, you are going to have a fun journey as a knight"); var dayFight = prompt("You are versing the goblins in a fight, you need to choose how many days you go to fight(1-100). Remember if you choose too little days then you won't have stayed long enough to win the fight but if you stay to long you have more of a chance of dying or getting injured").toLowerCase() if(dayFight == parseInt(dayFight, 10)) { console.log("You did not fight enough days, which has resulted in your kingdome loosing the war to the goblins."); } else if(dayFight > 60) { console.log("You have went to war to long and been seriously injured."); } else if(40 <= dayFight && dayFight <= 60) { console.log("You have been at war for the right amount of time. You have came out of the war with no injuries and you also have defieted the goblins!") } break; case "troll": console.log("Hello, you are going to have a fun journey as a troll"); var red = true; var green = true; var yellow = false; var blue = false; var houseRaid = prompt("You see four huts in the middle of the woods. You can raid two. There is a red hut, blue hut, yellow hut, and green hut. What is one color house that you want to raid?").toLowerCase() var doorPick = prompt("Do you want to enter throuhg the back door or front door?").toLowerCase() if(doorPick||houseRaid == "red"||"green" && "back door") { console.log("You raided these houses and left before any villagers would see you"); } else { console.log("You raided those houses, but one of them was a booby trap and you are now captured"); } break; case "human": console.log("Hello, you are going to have a fun journey as a human"); var reinforceFound = prompt("You know a storm is comming and you have to reinforce your hut, but you only have enough material to reinforce either your lower foundations or higher foundations. Which do you inforce? Higher or lower?").toLowerCase() if(reinforceFound == "lower") { console.log("The storms winds pushed down the top of your house and caved in your roof."); } else if(reinforceFound == "higher") { console.log("The storm did not do that much damage to your house due to your reinforced higher foundations. Good choice"); } else { console.log("sorry but that is not an option. Pick either 'higher', or 'lower'") } break; case "wizard": console.log("Hello, you are going to have a fun journey as a wizard"); var blood = true; var dust = true; var wings = false; var mushrooms = false; var postion = prompt("You are working on a new healing potion but you do not know what you need to add to finish it. You have 4 ingrediants; blood, dust, wings, mushrooms. Pick one too add. (WARNING: Pick carefully because if you choose the wrong ingerdiant, then your potion will be ruined.)").toLowerCase() if(postion == "wings" || postion == "mushroom") { console.log("You picked a bad ingrediant and now your potion is ruined."); } else if(postion = "dust" || "blood") { console.log("you picked the right ingrediant and your potion is okay") } else { console.log("sorry but that is not an option"); } break; default: console.log("Sorry but that is not a character in the game");};function callfun() {} thenthen function callfun() { var user = prompt("Are you a goblin, knight, troll, human, or wizard?").toLowerCase(); var name = prompt("What is your name?").toLowerCase(); var gender = prompt("Are you male or female?").toLowerCase();} 然后then function callfun() { var user = prompt("Are you a goblin, knight, troll, human, or wizard?").toLowerCase(); var name = prompt("What is your name?").toLowerCase(); var gender = prompt("Are you male or female?").toLowerCase(); switch (user) { break; }} 然后thenfunction callfun() { var user = prompt("Are you a goblin, knight, troll, human, or wizard?").toLowerCase(); var name = prompt("What is your name?").toLowerCase(); var gender = prompt("Are you male or female?").toLowerCase(); switch (user) { case "goblin": console.log("Hello, you are going to have a fun journey as a goblin"); var meetAnt = prompt("You run into a giant ant eater, you can attack or run. Do you choose to attack?").toLowerCase(); if (meetAnt == "yes") {} else if (meetAnt == "no") {} else {} break; }} 然后thenif (meetAnt == "yes") { console.log("you stab him in the eye, but then he hits you with his trunk. You are injured"); var finishAnt = prompt("you can either risk your life and finish him, or try to run away. Do you finish him?").toLowerCase(); if (finishAnt == "yes") { } else if (finishAnt == "no") { } else { }}else if (meetAnt == "no") {}else { console.log("Sorry that is not an option.")} 接下来是next isif (meetAnt == "yes") { console.log("you stab him in the eye, but then he hits you with his trunk. You are injured"); var finishAnt = prompt("you can either risk your life and finish him, or try to run away. Do you finish him?").toLowerCase(); if (finishAnt == "yes") { console.log("congratulations you have killed the aint eater!"); } else if (finishAnt == "no") { console.log("You run away and barley make it, you are badly hurt"); } else { console.log("sorry thats not an option"); }}else if (meetAnt == "no") { console.log("You run past the ant eater and get to saftey. You are very lucky")}else { console.log("Sorry that is not an option.")} Finally your code is hereFinally your code is herefunction callfun() { var user = prompt("Are you a goblin, knight, troll, human, or wizard?").toLowerCase(); var name = prompt("What is your name?").toLowerCase(); var gender = prompt("Are you male or female?").toLowerCase(); switch (user) { case "goblin": console.log("Hello, you are going to have a fun journey as a goblin"); var meetAnt = prompt("You run into a giant ant eater, you can attack or run. Do you choose to attack?").toLowerCase(); if (meetAnt == "yes") { console.log("you stab him in the eye, but then he hits you with his trunk. You are injured"); var finishAnt = prompt("you can either risk your life and finish him, or try to run away. Do you finish him?").toLowerCase(); if (finishAnt == "yes") { console.log("congratulations you have killed the aint eater!"); } else if (finishAnt == "no") { console.log("You run away and barley make it, you are badly hurt"); } else { console.log("sorry thats not an option"); } } else if (meetAnt == "no") { console.log("You run past the ant eater and get to saftey. You are very lucky") } else { console.log("Sorry that is not an option.") } break; case "knight": console.log("Hello, you are going to have a fun journey as a knight"); var dayFight = prompt("You are versing the goblins in a fight, you need to choose how many days you go to fight(1-100). Remember if you choose too little days then you won't have stayed long enough to win the fight but if you stay to long you have more of a chance of dying or getting injured").toLowerCase() if (dayFight = parseInt(dayFight, 10)) { console.log("You did not fight enough days, which has resulted in your kingdome loosing the war to the goblins."); } else if (dayFight > 60) { console.log("You have went to war to long and been seriously injured."); } else if (40 <= dayFight && dayFight <= 60) { console.log("You have been at war for the right amount of time. You have came out of the war with no injuries and you also have defieted the goblins!"); } break; case "troll": console.log("Hello, you are going to have a fun journey as a troll"); var red = true; var green = true; var yellow = false; var blue = false; var houseRaid = prompt("You see four huts in the middle of the woods. You can raid two. There is a red hut, blue hut, yellow hut, and green hut. What is one color house that you want to raid?").toLowerCase() var doorPick = prompt("Do you want to enter throuhg the back door or front door?").toLowerCase() if ((doorPick || houseRaid == "red") || ("green" && "back door")) { console.log("You raided these houses and left before any villagers would see you"); } else { console.log("You raided those houses, but one of them was a booby trap and you are now captured"); } break; case "human": console.log("Hello, you are going to have a fun journey as a human"); var reinforceFound = prompt("You know a storm is comming and you have to reinforce your hut, but you only have enough material to reinforce either your lower foundations or higher foundations. Which do you inforce? Higher or lower?").toLowerCase() if (reinforceFound == "lower") { console.log("The storms winds pushed down the top of your house and caved in your roof."); } else if (reinforceFound == "higher") { console.log("The storm did not do that much damage to your house due to your reinforced higher foundations. Good choice"); } else { console.log("sorry but that is not an option. Pick either 'higher', or 'lower'") } break; case "wizard": console.log("Hello, you are going to have a fun journey as a wizard"); var blood = true; var dust = true; var wings = false; var mushrooms = false; var postion = prompt("You are working on a new healing potion but you do not know what you need to add to finish it. You have 4 ingrediants; blood, dust, wings, mushrooms. Pick one too add. (WARNING: Pick carefully because if you choose the wrong ingerdiant, then your potion will be ruined.)").toLowerCase() if (postion == "wings" || postion == "mushroom") { console.log("You picked a bad ingrediant and now your potion is ruined."); } else if (postion = "dust" || "blood") { console.log("you picked the right ingrediant and your potion is okay") } else { console.log("sorry but that is not an option"); } break; default: console.log("Sorry but that is not a character in the game"); }; } 这篇关于在Javascript中修复关于我的案例的这个语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-18 09:52