05 - Introduction to Java - Exercise 4
Task 1 - Simple loop
Create a new project called
Loops
.Within the body of the
main
method, ask the user for a number.Print every number from 0 until the user’s chosen number.
Create another loop and now print only the numbers that are even.
Task 2 - FizzBuzz
Comment out previous code.
Ask user for a number -
n
.Print every number from 1 to
n
, but if a number is divisible by 3 print Fizz, if it’s divisible by 5 print Buzz, if it’s divisible by both print FizzBuzz.
Task 3 - Calculator
Reopen your Calculator project.
Can you change the code so that it runs until the user presses x? i.e. it asks the user for two number and an operator and performs the operation. After it prints out the result it asks the user if they want to do some more maths. If the user types ‘y' it will do the previous steps again, if the user presses 'x’ it will stop.
Task 4 - Odd/ Even
Create a new project called
OddEven
.Within the body of the main method, ask the user to choose - odd or even.
Remember what the user typed.
Ask the user for a number.
Randomly generate an integer number between 0 and 10.
Add it to the user’s number.
Is the result odd or even?
Did the user guess correctly?