Skip to lesson
MentorNxt home

Java Programming

Java Basics Mini Projects

Build a simple calculator

A calculator is the smallest real program: it shows a menu, does the maths and keeps running until the user picks Exit.

What you'll build

A console calculator. It shows a menu, reads two numbers and prints the answer with 2 decimals, like 12.00 + 3.50 = 15.50.

The menu comes back after every answer, until the user picks Exit. A wrong menu number or dividing by zero prints a clear message, never a wrong answer.

Sample run
=== Simple Calculator ===

1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Choose an option (1-5): 1
First number: 12
Second number: 3.5
12.00 + 3.50 = 15.50

1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Choose an option (1-5): 4
First number: 10
Second number: 0
Cannot divide by zero.

1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Choose an option (1-5): 5
Goodbye!

Requirements

Your program should do all of these.

  1. Show a menu: 1 Add, 2 Subtract, 3 Multiply, 4 Divide, 5 Exit.
  2. Read the user's choice and two numbers with Scanner.
  3. Print the result with 2 decimal places: 12.00 + 3.50 = 15.50, 100.00 - 45.50 = 54.50, 4.00 * 5.00 = 20.00 or 22.00 / 7.00 = 3.14.
  4. Show the menu again after every calculation, until the user picks 5.
  5. For a choice outside 1 to 5, print Please choose a number from 1 to 5. and show the menu again.
  6. Never divide by zero: print Cannot divide by zero. instead.
Build it step by step in the Build tab (5 milestones, each ends with a program that runs). Commit and push after each one: MentorNxt checks every push and shows the result in the Submit tab.

Next step

Start building

5 milestones