In today’s session we will look at how to interact with a user via keyboard inputs, make decisions using switch/case statements and if/else statements. Cheat sheets for this session are available on this website in the resources section.

How to make decisions

Applications and games need to make decisions about what actions to take when something happens.

For example:

  • If the number of lives left is 0 then print ‘Game Over’.
  • If age is less than 13 then redirect to google.com.

What other decisions do applications and games need to make?

Lets look at the first example. How would we code this? We use language that is very similar to normal English.

if ( lives == 0 ) {
System.out.println("Game Over");
}

Note the code in red. This is the conditional part of the if statement. This is the test that is performed to determine whether or not the code within the curly bracesĀ { } should be executed or not. If the conditional is true (are lives equal to 0) then the code in the braces will execute.

This statement tests for equality, but there are other types of comparisons that can be performed. Here are a few of the most common:

equal (==)
less than (<)
more than (>)
less than or equal (<=)
more than or equal (=>)

We can also check for unequally using the test !=

if ( lives != 9 ) {
System.out.println("You are not a cat");
}

What does this code do?

 

 

 

 

 

 

Answer: If lives is not equal to 9 then print the message ‘you are not a cat’.

Lets see another example:

if ( age <= 13 ) {
System.out.println("Sorry Too Young: Go to google.com");
}

What does this code do?

 

 

 

 

 

 

Answer: If age is less than or equal to 13 then print the message.

Lets expand this example with use of the else keyword.

if ( age <= 13 ) {
System.out.println("Sorry Too Young: Go to google.com");
} else {
System.out.println("Welcome");

}

 

Can you guess what this code does?

 

 

 

 

 

Answer: If age is less than or equal to 13 then print message ‘Sorry Too Young: Go to google.com’ else print ‘Welcome’.

Lets extend this a bit further:

if ( age <= 13 ) {
System.out.println("Sorry Too Young: Go to google.com");
} else if (age > 60) {
System.out.println("Sorry Too Old");
} else {
System.out.println("Welcome");
}

 

Can you guess what this code does?

 

 

 

 

 

 

Answer: If age is less than or equal to 13 then print message ‘Sorry Too Young: Go to google.com’ else if age is more than 60 print ‘Sorry Too Old’ else print ‘Welcome’.

Switch/Case

There is another way to make decisions. We can use switch/case to select one concrete action based on one decision. It is similar to if/elseif/else but cannot be used with > or <. Only equality can be tested.

int playerMove = 1;

switch (playerMove) {
    case 0:
        System.out.println("Player's move: Rock");
        break;
    case 1:
        System.out.println("Player's move: Paper");
        break;
    case 2:
        System.out.println("Player's move: Scissors");
        break;
}

The value of playerMove is used to determine which case is executed. Here playerMove is 1, therefore case 1 is selected and the code between case 1: and break; is executed and the text ‘Player’s move: Paper’ is printed to the console.

Alternatively a String can be used to determine the case selected.

String iceCream = "Strawberry";

switch (iceCream) {
    case "Chocolate":
        System.out.println("Delicious");
        break;
    case "Vanilla":
        System.out.println("Disgusting");
        break;
    case "Strawberry":
        System.out.println("Yummy My Fav");
        break;
}

 

Which case is selected and what is printed to the console?

 

 

 

 

 

 

Answer: As iceCream’s value is Strawberry the final case is chosen.

Getting input from the keyboard

We can write code that reads text typed by the user. This very useful when creating games that require text input from the user.

What type of games might make use of text input?

In and IDE we must use the following code to capture user input.

            Scanner in = new Scanner(System.in);
            String consoleInput = in.nextLine();

The user will type text into the console and when return is pressed the text typed is captured and stored in the String variable consoleInput.

Now that we have captured the user’s input we can it in our game to make decisions using an if or switch statement.

 

Tasks

Task 1: Write a program that asks a simple question to the user, captures the answer and responds to that question with an answer that depends on user’s answer.

HINT: What is you favourite ice cream? Answer: Chocolate. Computer responds: Delicious

Task 2: Write a simple guessing game. The computer chooses a random number and the user must guess what it is. If the choice is incorrect give the user a hint. If the guess is less than the computer’s number print to the console ‘Too Small’, if the guess is more than the computer’s number print to the console ‘Too Big’.

HINT: in order to repeatedly ask the user the same question put the code in the following infinite loop

        while (true) {

            // Code to repeat here

        }

when you want to exit use this code: System.exit(0);

Task 3: Write a Rock Paper Scissors game using all the constructs we have studied today.

Task 4: Extend the above task by writing a Rock Paper Scissor Lizard Spock game.

The rules are:

Scissors cuts Paper
Paper covers Rock
Rock crushes Lizard
Lizard poisons Spock
Spock smashes Scissors
Scissors decapitates Lizard
Lizard eats Paper
Paper disproves Spock
Spock vaporizes Rock
Rock crushes scissors