0
import java.util.Scanner;

public class Basic {
    public static void main(String[] args) {
        Scanner CalcType = new Scanner(System.in);
        double fnum, snum;
        String math;

        System.out.println("--------------------------------------------------");
        System.out.println("Welcome to the basic calculator made by codermadi!");
        System.out.println("--------------------------------------------------");
        System.out.println("Enter first number!");
        System.out.println("--------------------------------------------------");

        fnum = CalcType.nextDouble();

        System.out.println("--------------------------------------------------");
        System.out.println("Enter second number!");
        System.out.println("--------------------------------------------------");

        snum = CalcType.nextDouble();

        System.out.println("--------------------------------------------------");
        System.out.println("+ | - | / | x | %");
        System.out.println("--------------------------------------------------");

        math = CalcType.next();

        if (math == "+") {
            System.out.println(fnum + snum);
        }
        if (math == "-") {
            System.out.println(fnum - snum);
        }
        if (math == "/") {
            System.out.println(fnum / snum);
        }
        if (math == "*") {
            System.out.println(fnum * snum);
        }
        if (math == "x") {
            System.out.println(fnum * snum);
        }
        if (math == "%") {
            System.out.println(fnum % snum);
        }

        System.out.println("-------------------------------------------");
        System.out.println("Thank you for using codermadi's calculator!");
        System.out.println("-------------------------------------------");
    }

}

I have a problem, when I type the type of math I want my calculator to do it doesn't output the if statements.. My problem is at the bold text.

-

-

-

-

Had to add these because StackOverFlow wants me to add more details.

Jay Dangar
  • 3,271
  • 1
  • 16
  • 35
  • Maybe because what you wrote is wrong, you should learn some Java, and use `equals` for Objects – azro May 12 '18 at 08:49
  • The simplest fix would be to use equals, as suggested in [this answer](https://stackoverflow.com/a/513839/1110636). But what will suit your usage better is to convert all those ifs to a switch case statement: ` switch (math) { case "+": ...}` – Timir May 12 '18 at 09:02

1 Answers1

0

You shouldn't compare string objects using ==. Instead, use equals method. More about it here https://stackoverflow.com/a/513839/8294285

Martin P
  • 114
  • 4