2

I am trying to build a simple calculator that operates on Double values and performs:

+

-

*

/

negation

^2

for some reason, whichever operation i specify when running the program executes the addition function. So when I try to do

5 7 *

I get 12.0 and not 35.

Any suggestions?

I would also really like some help implementing a way to quit this program when the word "exit" is entered. I was thinking something like System.exit(0);

but Im not sure how to implement. this is my code so far:

import java.util.Scanner;

public class Calc {

public static void main(String[] args) 
{
    double num1;
    double num2;
    String operation;


    Scanner input = new Scanner(System.in);

    System.out.println ("Enter 'exit' to quit the calculator or 'help' for more options");
    System.out.println("Enter the first number:");
    num1 = input.nextInt();


    System.out.println ("Display:" +  num1);

    System.out.println("Enter the second number:");
    num2 = input.nextInt();

    System.out.println ("Display:" + num2);

    Scanner op = new Scanner(System.in);


    System.out.println("Please enter operation:");
    operation = op.next();

    if (operation == "+");
    {
        System.out.println((num1 + num2));
    }
    if  (operation == "-"){
    {
        System.out.println((num1 - num2));
    }

    if (operation == "/"){
    {
        System.out.println((num1 / num2));
    }
    if (operation == "*"){
    {
        System.out.println((num1 * num2));

    }

    }

    }       
Charlie Tidmarsh
  • 37
  • 1
  • 1
  • 2

4 Answers4

5

there is a semicolon at if (operation.equals("+")); so, System.out.println((num1 + num2)); will always work

And also replace, == with .equals("+") as == checks for reference, while equals checks for the value of the variable.

Matthias
  • 3,582
  • 2
  • 30
  • 41
HimanshuR
  • 1,390
  • 5
  • 16
  • 35
0

try this way Instead of using == use .equals()

import java.util.Scanner;

public class Calc {

public static void main(String[] args) 
{
    double num1;
    double num2;
    String operation;


    Scanner input = new Scanner(System.in);

    System.out.println ("Enter 'exit' to quit the calculator or 'help' for more options");
    System.out.println("Enter the first number:");
    num1 = input.nextInt();


    System.out.println ("Display:" +  num1);

    System.out.println("Enter the second number:");
    num2 = input.nextInt();

    System.out.println ("Display:" + num2);

    Scanner op = new Scanner(System.in);


    System.out.println("Please enter operation:");
    operation = op.next();

    if (operation.equals("+"))
    {
        System.out.println((num1 + num2));
    }
    if  (operation.equals("-"))
    {
        System.out.println((num1 - num2));
    }

    if (operation.equals("/")){
    {
        System.out.println((num1 / num2));
    }
    if (operation.equals( "*")){
    {
        System.out.println((num1 * num2));

    }

    }

    }       
SpringLearner
  • 13,738
  • 20
  • 78
  • 116
0

please change operation == to operation.equals("your value")

ggDeGreat
  • 1,098
  • 1
  • 17
  • 33
0

There are at least three main issues with your code...

Firstly, String comparison in Java is done using .equals not ==

Secondly...

if (operation == "+");

Has a semi-colon at the end, which basically means that even if the condition is meet, it will not run anything, but will skip to the next execution block, which is ...

{
    System.out.println((num1 + num2));
}

Thirdly, you have an extra open brace ({) here...

if  (operation == "-"){
{

Here...

if (operation == "/"){
{

And here...

if (operation == "*"){
{

Which is going to completely screw up your logic

You should also make use of else-if blocks as well...

Instead, try something like...

if ("+".equals(operation))//;
{
    System.out.println((num1 + num2));
} 
else if ("-".equals(operation)) //{
{
    System.out.println((num1 - num2));
}
else if ("/".equals(operation)) //{
{
    System.out.println((num1 / num2));
}
else if ("*".equals(operation)) //{
{
    System.out.println((num1 * num2));
}

I've commented out the problem areas so you can see where they exists before hand

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366