2015-06-19 67 views
-3

我正在寫一個熱門或冷的應用程序。我需要一個switch語句來決定用戶是熱的還是冷的。這裏是我到目前爲止的代碼:我需要檢查switch語句是否相等

switch(true) { 
 
     \t case (userGuess/secretNumber === 1): 
 
     \t \t setFeedback("You win"); 
 
      \t finish = true; 
 
      \t break; 
 
      case ((userGuess - secretNumber) > 50): 
 
      \t setFeedback("You're ice cold!"); 
 
      \t break; 
 
      case ((userGuess - secretNumber) > 30): 
 
      \t setFeedback("You're cold!"); 
 
      \t break; 
 
      case ((userGuess - secretNumber) > 20): 
 
      \t setFeedback("You're warm!"); 
 
      \t break; 
 
      case ((userGuess - secretNumber) > 10): 
 
      \t setFeedback("You're hot!"); 
 
      \t break; 
 
      case ((userGuess - secretNumber) > 0): 
 
      \t setFeedback("You're blazing hot!"); 
 
      \t break; 
 
     }

編輯: 我試圖替換以下,如果用一個開關/ else語句。請告訴我,如果可能,如果是這樣的話:如何。這裏的舊代碼:

if (userGuess/secretNumber === 1){ 
 
      setFeedback("You win"); 
 
      finish = true; 
 
      } else if ((secretNumber - userGuess) > 60.5){ 
 
      setFeedback("Wow! You are freezing!"); 
 
      } else if ((secretNumber - userGuess) > 55.5){ 
 
      setFeedback("Wow! You better put on a jacket cause its super cold!"); 
 
      } else if ((secretNumber - userGuess) > 50.5){ 
 
      setFeedback("Its is super cold man!"); 
 
      } else if ((secretNumber - userGuess) > 40.5) { 
 
      setFeedback("Now you are cold!"); 
 
      } else if ((secretNumber - userGuess) > 30.5) { 
 
      setFeedback("It's getting warm around here"); 
 
      } else if((secretNumber - userGuess) > 20.5) { 
 
      setFeedback("It's getting very warm in here!"); 
 
      } else if((secretNumber - userGuess) > 15.5) { 
 
      setFeedback("It's getting very very warm in here!"); 
 
      } else if ((secretNumber - userGuess) > 7.5){ 
 
      setFeedback("It's hot!"); 
 
      } else if ((secretNumber - userGuess) > 5.5){ 
 
      setFeedback("It is very hot here!"); 
 
      } else if ((secretNumber - userGuess) > 1.5){ 
 
      setFeedback("I am burning here!!"); 
 
      } else if ((secretNumber - userGuess) > 0.5){ 
 
      setFeedback("Its is hotter than the sun!!!!!"); 
 
     } else { 
 
     }

+1

這是過於寬泛,正是你問? – Brian

+0

我試圖替換這個if/else語句: –

+0

試試閱讀:http://www.w3schools.com/js/js_switch.asp – Brian

回答

0
 int value = secretNumber-userGuess; 

     if ((userGuess/secretNumber) == 1) 
     { 
      setFeedback("You win"); 
      finish = true; 
     } 
     else if (value > 60.5) 
      setFeedback("Wow! You are freezing!"); 
     else if (value > 55.5) 
      setFeedback("Wow! You better put on a jacket cause its super cold!"); 
     else if (value > 50.5) 
      setFeedback("Its is super cold man!"); 
     else if (value > 40.5) 
      setFeedback("Now you are cold!"); 
     else if (value > 30.5) 
      setFeedback("It's getting warm around here"); 
     else if (value > 20.5) 
      setFeedback("It's getting very warm in here!"); 
     else if(value > 15.5) 
      setFeedback("It's getting very very warm in here!"); 
     else if (value > 7.5) 
      setFeedback("It's hot!"); 
     else if (value > 5.5) 
      setFeedback("It is very hot here!"); 
     else if (value > 1.5) 
      setFeedback("I am burning here!!"); 
     else if (value > 0.5) 
      setFeedback("Its is hotter than the sun!!!!!"); 
    else { 
    } 
相關問題