Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialGianpiero Gaeta
1,697 Pointscan we use the switch istead of if?
switch (randomNumber){ case 0:fact = "Ants stretch when they wake up in the morning"; break; case 1:fact = "Ostriches can run faster than houses"; break; case 2:fact = "Olympic gold medals are actually made mostly of silver"; break; } factLabel.setText(fact);
1 Answer
Steve Hunter
57,712 PointsYes you could.
However, you would need to hard-code every single option. The project will move away from this method of selecting the fact; it is just a passing point. You'll soon use a method that means you can have limitless numbers of facts and one will be randomly selected, without having to code a case
for each fact.
Keep on going!
Steve.