# Thread: Need help with Calculator

1. Member Join Date
Dec 2011
Posts
3
Rep Power
0

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

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

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  Reply With Quote

2. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## Re: Need help with Calculator

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:

Java 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) {
}
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:

Java Code:
```if(response == 1) {

} else if(response == 2) {

} else //etc```
Have you come across "switch" statements, because that's what we really have here.  Reply With Quote

3. Member Join Date
Dec 2011
Posts
3
Rep Power
0

## 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: "?  Reply With Quote

4. Member Join Date
Oct 2011
Posts
79
Rep Power
0

## Re: Need help with Calculator

use a loop  Reply With Quote

5. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## 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.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•