问题
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