Ok so I'm teaching myself (along with the help of some youtube videos) java. So after a few videos I thought I would try and test my skills by setting up a calculator. now I had done this before with lua but its a somewhat diffrent ball game here. my issue is that no matter how i set up any variables or if/else statements, the program always ends up just skipping over the else ifs. i put a final else at the base of the code to see if it was even reaching the bottom and it was definitly, so heres my code
import java.util.Scanner;
public class calculator {
public static void main(String[] args){
double Answer;
String op;
double num1;
double num2;
Scanner input = new Scanner(System.in);
System.out.print("What opperation would you like to preform? +,-,*,/, :");
op = input.nextLine();
System.out.print("What is the first number? : ");
num1 = input.nextDouble();
System.out.print("And the seccond number? : ");
num2 = input.nextDouble();
if(op == "+"){
Answer = (num1 + num2);
System.out.print(Answer);
}else if(op == "-"){
Answer = num1 - num2;
System.out.print(Answer);
}else if(op == "*"){
Answer = num1 * num2;
System.out.print(Answer);
}else if(op == "/"){
Answer = num1 / num2;
System.out.print(Answer);
}else{
System.out.print("ilikecake");
}
}
}
as you can see, i used a string for the opperation (op) variable due to the flexability of strings in this particular application, also, i couldn't figure out how to get scanner to accept a char variable. i keep getting the string can not be converted to char error so i just set it up as a string. if you can help thank you! also please ignore any akward formatting from the post, i couldn't figure out how to make the code appear properly so i did my best :/