我需要检查switch语句是否相等

时间:2015-06-19 02:49:10

标签: javascript switch-statement equality

我正在写一个热门应用或冷应用。我需要一个switch语句来决定用户是热还是冷。到目前为止,这是我的代码:



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




编辑:  我试图用开关替换以下if / 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 个答案:

答案 0 :(得分: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 {
    }