I'm new to java programming. I tried to make calculator that can do 4 basic math operations using if statement. However I don't have it working as expected. When trying to parse operator, it just finishes with else statement.
I guess I have not properly formatted if statement ?
Any help is greatly appreciated.
Thanks.
import java.util.Scanner;
import java.lang.Object;
public class calc {
public static void main(String args[]) {
System.out.println("Test kalkulator za sabiranje");
Scanner keyboard = new Scanner(System.in);
double fnum, snum, res;
String ch = "";
System.out.println("Enter first number: ");
fnum = keyboard.nextDouble();
System.out.println("Enter operation: ");
ch = keyboard.next();
if( ch == "+") {
System.out.println("Enter second number: ");
snum = keyboard.nextDouble();
res = fnum + snum;
System.out.println("Result is: "+ res);
}
else if ( ch == "-") {
System.out.println("Enter second number: ");
snum = keyboard.nextDouble();
res = fnum - snum;
System.out.println("Result is: "+ res);
}
else if ( ch == "/") {
System.out.println("Enter second number: ");
snum = keyboard.nextDouble();
res = fnum / snum;
System.out.println("Result is: "+ res);
}
else if( ch == "*") {
System.out.println("Enter second number: ");
snum = keyboard.nextDouble();
res = fnum * snum;
System.out.println("Result is: "+ res);
}
else {
System.out.println("You entered wrong operator, please try again");
}
keyboard.close();
}
}