0
Why does my if else statement not execute with addition if the user input is [c = +, a = 2 & b = 3]
Why does my if else statement not execute with addition if the user input is [c = +, a = 2 & b = 3] My code keeps executing the else block instead of the if block (when + is inputted). The program has a lot more to be finished but I need help on this one aspect please and thanks. PARTIAL CODE: public static double calculation(String c, double a, double b) { double result = 0; if(c == "+") { result = add(a, b); } else if(c == "-") { result = subtract(a, b); } else if(c == "*") { result = multiply(a, b); } else{ result = divide(a, b); } return result; } Everything runs properly but it keeps doing division instead of addition
1 Respuesta
+ 3
Have you tried debugging by putting the following as the first step in your method?
System.out.print(c);
It could be that the value of 'c' is not actually "+". If it even has a space it's not equal.
If that doesn't help, you can add your full code to this question by posting the link. That way, people here can run it and see what happens :)