2014-06-28 67 views
-5

有五種情況被接受。仍然是Java的新手,所以這是一個有點奇怪的例子。據我可以告訴(我已閱讀代碼至少兩次)它應該工作得很好,除非雙開關使它不起作用...我得到錯誤「SyntaxError:意外的標記案例」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: 

} 

據我所知,這應該起作用....

+0

哇花花公子,給一些代碼格式:/,並解釋爲什麼它應該工作,並在那裏它不。 –

+0

這不是Java。可能是JavaScript,這是一種非常不同的語言。 –

+0

通常錯誤是伴隨着一個行號。你看/圍繞該線,找到錯誤並修復它。 –

回答

2

在這部分(「買它」的情況下),你錯過了這個結束大括號。

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; 

代碼工作here

+2

最後一個'default:'應該在前面的語句之前。 (不知道JavaScript是否會檢查這種事情。) –

+0

實際上不檢查,但是它是一個編程錯誤。 :) –

相關問題