可能重复:
如何比较 Java 中的字符串?
为什么我的程序不能使用我分配的字符串运算符来计算两个整数?由于某种原因,就好像程序不接受用户的输入一样。
import java.io.*;
public class IntCalc {
public static void main (String [] args) throws IOException {
BufferedReader kb = new BufferedReader (new InputStreamReader (System.in));
System.out.println ("This program performs an integer calculation of your choice.");
System.out.println ("Enter an integer: ");
int x = Integer.parseInt (kb.readLine());
System.out.println ("Enter a second integer: ");
int y = Integer.parseInt (kb.readLine());
System.out.print ("Would you like to find the sum, difference, product, quotient, or remainder of your product?: ");
String operator = kb.readLine();
int finalNum;
if (operator == "sum") {
int finalNum = (x + y);
} else if (operator == "difference") {
int finalNum = (x - y);
} else if (operator == "product") {
int finalNum = (x * y);
} else if (operator == "remainder") {
int finalNum = (x % y);
}
System.out.print ("The " + operator + " of your two integers is " + finalNum ".");
}
}