I'm trying to create a command/console calculator program but I'm getting stuck with the if statements for the operators.
I know this isn't the easiest (or even the correct) way to create a calculator program but I'm doing it this way so I can practice user Output, Input, If statements and calculations.
I was thinking that for the operator variable it would be type char, but if I do it that way I cant get the user input. Whats wrong with the following code?
package calculator;
import static java.lang.System.in ;
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println ("Welcome to the calculator!");
System.out.print ("Enter a calculation: ");
int a;
int b;
String Op; //Operator
a = in.nextInt();
b = in.nextInt();
Op = in.next();
if (Op == '*') { a*b ;}
if (Op =='/') { a/b ;}
if (Op == '+') {a+b;}
if (Op == '-') {a-b;}
}
}
I haven't finished it off yet (as you can see there isn't any output) but that's because I cant get the if statements to work.
The errors I'm getting are:
error: not a statement
if (Op == '-') {a-b;}
This is for every if statement.