From the course: Java 8+ Essential Training: Syntax and Structure

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Solution: A more complex calculator

Solution: A more complex calculator - Java Tutorial

From the course: Java 8+ Essential Training: Syntax and Structure

Start my 1-month free trial

Solution: A more complex calculator

- [Instructor] In this challenge, the job is to create a calculator that accepts two numeric values, and then asks the user what kind of operation they want to execute. They can type in a plus, a minus, a times or a divide symbol, and the result is whatever operation is requested. Here is my solution to the challenge. First of all, I've extracted my code to receive input from the console to a separate method called get input. This is exactly the same code I used in a previous video. I'm calling that method three times with three different prompts, and saving the responses as three different string variables. Next I'm declaring a variable typed as a double, named result, and setting it to zero. I declare it up here so that I can then affect that value anywhere in the switch statement, but then refer to the value later on. If you were to declare the variable within the switch statement, it wouldn't be available after the switch statement is complete. Next, I evaluate the operation that…

Contents