Actionscript if / else syntax Question

↘锁芯ラ 提交于 2019-12-24 06:49:37

问题


Which of the following best translates the English statement "If it's rainy, we will watch a movie. Otherwise we will go to the park."

   a. if (rainy = true) { gotoAndStop ("movie"); }
   b. if (rainy == true) { gotoAndStop ("movie"); }
   c. if (rainy = true) { gotoAndStop ("movie"); } else { gotoAndStop ("park"); }
   d. if (rainy == true) { gotoAndStop ("movie"); } else { gotoAndStop ("park"); }

My answer would be "d" - is that correct?


回答1:


Yes, 'd' is the correct answer.

The difference between = and == is that == compares and returns a Boolean (true or false) which you operate upon (called 'branching').

= is called the assignment operator and while perfectly valid code to write, is not what you normally want to use in an if statement.

if(x = 5) {
    doStuff();
} 

Basically means "put 5 instead of x; if x is non-zero call doStuff".

Another thing to note is when it comes to booleans, it's "safer" to write

if (rainy) {
    gotoAndStop("movie");
} else {
    gotoAndStop("park);
}



回答2:


This is cool too:

gotoAndStop(rainy ? "movie" : "park");



回答3:


or...try this, does the same.... but looks sexy :)

var activity:String = (rainy) ? "movie": "park";
gotoAndStop(activity);


来源:https://stackoverflow.com/questions/1324272/actionscript-if-else-syntax-question

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!