- LHS Coding Club

advertisement
Let’s learn some Java!!!
Lesson 3: if…. else
In Java, we can control the "flow" of the program by using conditional statements.
Let's take a look at this simple example.
This is a pretty simple class. Let me explain some of the things we have here.
1. class DrinkingAgeTest creates a class called DrinkingAgeTest.
2. We then create a static integer age 3. Of course we create a main method, as all Java
programs will look for this as a starting point.
4. Now the interesting parts:
Meet the if statement. It is written like so:
When Java encounters the if statement, it checks each condition to see if it is satisfied
(true).
If so, it carries out everything inside the braces for that if statement. If not, the
statement is completely skipped.
Applying this to the example class, the compiler checks if age, which is 18, is greater
than or equal to 21.
It is not, so it skips the line:
System.out.println(“You may drink”);
Since none of the previous conditions are met, it automatically goes into the else
statement, which outputs:
Assignment: Tweak this program to test if a kid is old enough to
play our videogame.
YOU HAVE FIVE MINUTES TO DO THIS!!!!
(It should take you like two minutes)
Feeling confident? Let’s make a game worthy of your coding skills
Now let's simulate probability, starting with a simple example.
import java.util.Random;
//Let's simulate a coin toss!
public class CoinToss {
public static void main(String[] args){
Random rand = new Random();
int result = rand.nextInt(2); // create a value between zero and 2( not including 2)
if (result == 0){
System.out.println("heads");
}
else if(result == 1){
System.out.println("tails");
}
}
This is also a pretty straightforward class. We use the random number generator to
create a number between 0 and 1 (inclusive) and set it equal to a newly created integer:
result.
We arbitrarily decide that the value of 1 is equal to heads and the value of 0 is equal to
tails. Now we test the value of result using else if statements (mutually exclusive events)
and display the appropriate string (text).
By the way….
Here are all six relational operators:
Lesson #1-11: Conditional Operators and Our First Game
In this lesson, we are going to create a simple game. Oh, don't get too excited. By
simple, I mean simple.
Before we do that, here's a quick lesson on conditional operators:
When you have an if statement like below:
Then if either conditionOne or conditionTwo is true, it will invoke the "doThis()"
method;
When you have a statement like this:
Now back to our game!
Here is how the game works. You roll a 6-sided die. If you land a 1 or 2 you lose. If you
get a 3, 4, or 5, you roll again. If you get a 6, you win.
To write good programs, you have to plan beforehand. You should think the following:
1. I need a random number generator.
2. I should test if:
I. a number is less than or equal to 2.
II. if not, I should test whether if that number is between 3 and 5 (inclusive)
III. if not, I should test whether if that number is 6.
3. I should carry out an appropriate response.
Simple. Now let's create this class together.
Flip the page to see the right answer
Download