Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Conditionals
- public class FunFactsActivity extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_fun_facts);
- final TextView factLabel = (TextView) findViewById(R.id.factTextView);
- Button showFactButton = (Button) findViewById(R.id.showFactButton);
- View.OnClickListener listenerName = new View.onClickListener(){
- @Override
- public void onClick(View view) {
- String fact = "";
- Random randomGenerator = new Random ();
- int randomNumber = randomGenerator.nextInt(3);
- 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);
- }
- };
- showFactButton.setOnClickListener(listenerName);
- }
- }
- //-----------------------------------------------------------------------------------------------------------------------------------------
- public class FunFactsActivity extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_fun_facts);
- final TextView factLabel = (TextView) findViewById(R.id.factTextView);
- Button showFactButton = (Button) findViewById(R.id.showFactButton);
- View.OnClickListener listenerName = new View.onClickListener(){
- @Override
- public void onClick(View view) {
- String fact = "";
- Random randomGenerator = new Random ();
- 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”
- */
- // Conditional = if then statement:
- /* if(condition) {
- Java statements to be executed
- }
- */
- // if randomNumber equals 0 then
- if (randomNumber == 0) { // Hint: using only one "=" would be assigning the value "0" to "randomNumber"
- // set fact equals to ants fact
- 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 { //else w/o the if == if everything fails, this will come up
- fact = "Sorry there was an error";
- }
- // Update the label with our dynamic fact
- factLabel.setText(fact);
- }
- };
- showFactButton.setOnClickListener(listenerName);
- }
- }
- /*
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement