# Need help with Calculator

• 12-10-2011, 01:14 AM
Joshua4missions
Need help with Calculator
So some days ago I started to actually learn Java. I learned a few commands and syntax and decided to try my hand at making a calculator without following an online guide.

Code:

```import java.util.Scanner; public class Calculator {                 public static void main(String[] args) {                                 //Calculator                                 //Final formula: firstvalue + secondvalue = answer                                                 //Display message asking for first number.                 System.out.println("Please input first value: ");                 Scanner scan = new Scanner(System.in);                 int firstvalue = scan.nextInt();                 //Pick what mathematics function.                 System.out.println("Type: \n '1' for Addition \n '2' for Subtraction \n '3' for Multiplication \n '4' for Division ");                 //Based on what function you chose, the function will then be carried out.                                 //Addition.                 int addition = scan.nextInt();                 if (addition == 1) {                         System.out.println("Addition selected: Input second number please: " + firstvalue + " + ?\n");                         int secondvalue = scan.nextInt();                         int answer = firstvalue + secondvalue;                         System.out.println(firstvalue + " + " + secondvalue + " = " + answer);                 }                                 //Subtraction.                 int subtraction = scan.nextInt();                 if (subtraction == 2) {                         System.out.println("Subtraction selected: Input second number please: " + firstvalue + " - ?\n");                         int secondvalue = scan.nextInt();                         int answer = firstvalue - secondvalue;                         System.out.println(firstvalue + " - " + secondvalue + " = " + answer);                 }                 //Multiplication.                 int multiplication = scan.nextInt();                 if (multiplication == 3) {                         System.out.println("Multiplication selected: Input second number please: " + firstvalue + " x ?\n");                         int secondvalue = scan.nextInt();                         int answer = firstvalue * secondvalue;                         System.out.println(firstvalue + " x " + secondvalue + " = " + answer);                 }                 //Division.                 int division = scan.nextInt();                 if (division == 4) {                         System.out.println("Division selected: Input second number please: " + firstvalue + " / ?\n");                         int secondvalue = scan.nextInt();                         int answer = firstvalue / secondvalue;                         System.out.println(firstvalue + " / " + secondvalue + " = " + answer);                 }                                         }         }```

The code works, but my dilemma is that only the addition section works. This is probably due to the fact that code works it's way down, and since the addition condition is not met, none of the other conditions work. How would I make it so that all of the Addition, Subtraction, Multiplication, and Division conditions are activated at the same time?

-Joshua
• 12-10-2011, 01:49 AM
pbrockway2
Re: Need help with Calculator
Quote:

only the addition section works. This is probably due to the fact that code works it's way down, and since the addition condition is not met, none of the other conditions work.

A better approach, since you prompt the user once you should get their response once and then deal with it:

Code:

```    // prompt user and get response System.out.println("Type: \n '1' for Addition etc"); int response = scan.nextInt();     // deal with the response they give if(response == 1) {     // addition code here } if(response == 2) {     // subtraction code here } // etc - what happens if they enter 42?```
Note that whatever they enter it will be 1 OR 2 OR 3 etc but never more than one thing. So we could write:

Code:

```if(response == 1) { } else if(response == 2) { } else //etc```
Have you come across "switch" statements, because that's what we really have here.
• 12-10-2011, 03:37 AM
Joshua4missions
Re: Need help with Calculator
Okay, thanks. I made that change and also changed the "int" to "double". Thanks again. :)

Now how would I reset the entire code so that once they get their answer it starts again at the top with "Please input first value: "?
• 12-10-2011, 03:39 AM
jiffi
Re: Need help with Calculator
use a loop
• 12-10-2011, 03:44 AM
pbrockway2
Re: Need help with Calculator
I agree, use a loop.

To elaborate just a little, it would be a good idea if the main() method were not allowed to grow like topsy. main() might just consist of a loop whose body consists of a call to another method to run the calculator and a prompt asking if the user wants to continue.