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 trialAndrew D.
2,937 PointsThird Fact(Silver Medals) Not Being Generated, Only First Two
Hey guys. I'll post my code below. For whatever reason the third fact about gold medals being comprised mostly of silver isn't being randomly generating, only the first two facts. I thought it had something to do with nextInt being set to 3 rather than 2(since it starts at 0 in Java) but my code is exactly like Ben's:
public void onClick(View v) {
//The button was clicked, so update the fact label with a new fact
String fact = "";
//Randomly select a fact
Random randomGenerator = new Random(); // Construct a new Random number generator
int randomNumber = randomGenerator.nextInt(3);
/* Convert the randomNumber to a text fact
* 0 = Ants stretch when they wake up in the morning.
* 1 = Ostriches can run faster than horses.
* 2 = Olympic gold medals are actually made mostly of silver.
*/
//if randomNumber is equal to 0, then set equal to ants fact
if (randomNumber == 0) {
fact = "Ants stretch when they wake up in the morning.";
}
else if (randomNumber == 1) {
fact = "Ostriches can run faster than horses.";
}
else if (randomNumber == 2) {
fact = "Olympic gold medals are actually made mostly of silver.";
}
else {
fact = "Sorry! There was an error.";
}
factLabel.setText(fact);
}
};
Any ideas? Thank you!
Andrew D.
2,937 PointsAndrew D.
2,937 PointsDisregard! I installed genymotion to get a faster emulator and now it works properly. But on the old emulator, I really did click 'Show another fun fact' about 20 times before I decided the code must not be working. It was probably a buggy emulator.