Categories

# 2 – Selection (Level 2)

## 1 Sample Code

### 1.1 Simple Selection

```System.out.println("Enter your surname");
Scanner scan = new Scanner(System.in);
String surname = scan.next();
System.out.println("Are you M or F?");
String sex = scan.next();
if(sex.equals("M"))
{
System.out.println("Hello Mr " + surname);
}
else
{
System.out.println("Hello Ms " + surname);
}
```

### 1.2 Operators

```public class  div37
{
public static void main (String args [])
{
int x = 21;
if (x%3 == 0 & x%7 == 0)
{
System.out.println("Number divisible by 3 and 7");
}
}
}
```

## 2 Exercises

1. Prompt the user as follows: “What’s the capital of France?” Output “Correct” if they enter “Paris”, output “Incorrect” otherwise
2. Prompt the user as follows: “Name a month that starts with the letter A”: Output “Correct” if they enter “April” or “August”, output “Incorrect” otherwise
3. Prompt the user as follows: “Name a Beatle”. Output “Correct” if they enter “John”, “Paul”, “George” or “Ringo”, output “Incorrect” otherwise
4. An online whisky shop charges for shipping as follows: One bottle, £5.99; two to five bottles, £7; more than five bottles, free. Prompt the user to enter the number of bottles bought and output the shipping cost.
5. An online bookshop charges shipping as follows: Orders less than £10, £2.99; orders £10 and over, free; add on £2.50 for all orders if next day delivery is selected. Prompt the user to enter the cost of the order, and then prompt for next day delivery. Output the shipping cost.
6. Prompt the user to enter a number. Output if the number is odd or even.
7. Prompt the user to enter a number. Output Fizz if the number is divisible by 3, otherwise just output the number
8. Extend yesterday’s problem so that the computer will output Fizz if the number is divisible by 3, output Buzz if the number is divisible by 5 and otherwise just output the number.
9. Now extend yesterday’s problem further so that the computer will output Fizz if the number is divisible by 3, output Buzz if the number is divisible by 5, output Fizz Buzz if the number is divisible by both 5 and 3 and otherwise just output the number.

This site uses Akismet to reduce spam. Learn how your comment data is processed.