/
05 - Introduction to Java - Exercise 4

05 - Introduction to Java - Exercise 4

Task 1 - Simple loop

  1. Create a new project called Loops.

  2. Within the body of the main method, ask the user for a number.

  3. Print every number from 0 until the user’s chosen number.

  4. Create another loop and now print only the numbers that are even.

Task 2 - FizzBuzz

  1. Comment out previous code.

  2. Ask user for a number - n.

  3. 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

  1. Reopen your Calculator project.

  2. 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

  1. Create a new project called OddEven.

  2. Within the body of the main method, ask the user to choose - odd or even.

  3. Remember what the user typed.

  4. Ask the user for a number.

  5. Randomly generate an integer number between 0 and 10.

  6. Add it to the user’s number.

  7. Is the result odd or even?

  8. Did the user guess correctly?

Related content