Advertisement
yo2man

Conditionals (if then)

Jun 20th, 2015
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.08 KB | None | 0 0
  1. //Conditionals
  2.  
  3. public class FunFactsActivity extends Activity {
  4. @Override
  5.     public void onCreate(Bundle savedInstanceState) {
  6.         super.onCreate(savedInstanceState);
  7.         setContentView(R.layout.activity_fun_facts);
  8.  
  9.     final TextView factLabel = (TextView) findViewById(R.id.factTextView);
  10.     Button showFactButton = (Button) findViewById(R.id.showFactButton);
  11.     View.OnClickListener listenerName = new View.onClickListener(){
  12.        
  13.         @Override
  14.         public void onClick(View view) {
  15.         String fact = "";
  16.                 Random randomGenerator = new Random ();
  17.         int randomNumber = randomGenerator.nextInt(3);
  18.        
  19.         if (randomNumber == 0) {                
  20.             fact = "Ants stretch when they wake up in the morning";
  21.         }
  22.         else if (randomNumber == 1) {
  23.             fact = “Ostriches can run faster than horses.”;
  24.         }
  25.         else if (randomNumber == 2) {
  26.             fact = "Olympic gold medals are actually made mostly of silver";
  27.         }
  28.         else {                  
  29.             fact = "Sorry there was an error";
  30.         }
  31.        
  32.  
  33.         factLabel.setText(fact);
  34.                        }
  35.         };
  36.                
  37.         showFactButton.setOnClickListener(listenerName);
  38.     }
  39. }
  40.  
  41. //-----------------------------------------------------------------------------------------------------------------------------------------
  42.  
  43. public class FunFactsActivity extends Activity {
  44. @Override
  45.     public void onCreate(Bundle savedInstanceState) {
  46.         super.onCreate(savedInstanceState);
  47.         setContentView(R.layout.activity_fun_facts);
  48.  
  49.     final TextView factLabel = (TextView) findViewById(R.id.factTextView);
  50.     Button showFactButton = (Button) findViewById(R.id.showFactButton);
  51.     View.OnClickListener listenerName = new View.onClickListener(){
  52.        
  53.         @Override
  54.         public void onClick(View view) {
  55.         String fact = "";
  56.                 Random randomGenerator = new Random ();
  57.         int randomNumber = randomGenerator.nextInt(3);
  58.        
  59.  
  60.               /*Convert the randomNumber to a text fact
  61.                * 0 = “Ants stretch when they wake up in the morning.”
  62.                * 1 = “Ostriches can run faster than horses.”
  63.                * 2 = “Olympic gold medals are actually made mostly of silver”
  64.                */
  65.  
  66.         // Conditional = if then statement:
  67.         /* if(condition) {
  68.             Java statements to be executed
  69.            }
  70.         */
  71.  
  72.         // if randomNumber equals 0 then
  73.         if (randomNumber == 0) {                  // Hint: using only one "=" would be assigning the value "0" to "randomNumber"
  74.             // set fact equals to ants fact
  75.             fact = "Ants stretch when they wake up in the morning";
  76.         }
  77.         else if (randomNumber == 1) {
  78.             fact = “Ostriches can run faster than horses.”;
  79.         }
  80.         else if (randomNumber == 2) {
  81.             fact = "Olympic gold medals are actually made mostly of silver";
  82.         }
  83.         else {                    //else w/o the if == if everything fails, this will come up
  84.             fact = "Sorry there was an error";
  85.         }
  86.        
  87.        
  88.         // Update the label with our dynamic fact
  89.         factLabel.setText(fact);
  90.                        }
  91.         };
  92.                
  93.         showFactButton.setOnClickListener(listenerName);
  94.     }
  95. }
  96.  
  97.  
  98.  
  99.  
  100. /*
  101.  
  102.  
  103.  
  104. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement