-3

I am new to Java. I wrote this program and when I run it, it always gives me that the answer is 0.0

Can you tell me what I did wrong?

import java.util.Scanner;
public class Learnclass {
    public static void main(String[] args) {
        Double fnum, snum;
        Double ans = 0.0;
        String opr;
        Scanner getIn = new Scanner(System.in);


        System.out.print("Enter your first number: ");
        fnum = getIn.nextDouble();
        System.out.print("Enter your second number: ");
        snum = getIn.nextDouble();
        System.out.print("Enter the operation: ");
        opr = getIn.next();


    if(opr == "add") {
        ans = fnum + snum;
   } 

    System.out.print("Answer is " + ans);
}

}

1 Answers1

1

...replace if(opr == "add") with if("add".equals(opr)), you want to check that the string are equals not that they are the same object!