我的类实现了RPN计算器的一个非常简单的原型。
以下构造不起作用。为什么?我在这里做错了什么?
public boolean executeCommand(String command) {
if(command == "+") { add(); return true; }else
if(command == "-") { subtrair(); return true; }else
if(command == "*") { multiplicar(); return true; }else
if(command == "/") { dividir(); return true; }else
{
System.out.println("The command does not exist.");
return false;
}
}
无论字符串包含什么,输出始终是,
The command does not exist.
为什么?我真的不明白!如果有人能解释一下,我将不胜感激!
更详细
有问题的方法是:
public boolean executeCommand(String command) {
Scanner str = new Scanner(command);
if (str.hasNextDouble()) {
dataStack.push(str.nextDouble());
return true;
} else {
System.out.format(" DEBUG: command: %s$%n", command);
if(command == "+") { add(); return true; }else
if(command == "-") { subtract(); return true; }else
if(command == "*") { multiply(); return true; }else
if(command == "/") { divide(); return true; }else
if(command == ".") { print(); return true; }else
if(command == ".s") { showStack(); return true; }else
if(command == "exit") { exit(); return true; }else
{
System.out.println("The command does not exist.");
return false;
}
}
}
对于我投入的任何输入(当然,数字除外),结果是:
DEBUG: command: [COMMAND HERE]$
The command does not exist.
源代码
我删除了一些不相关的源代码;(即一些方法,包名),但它仍然是可编译和可运行的:
import java.util.Scanner;
import java.util.LinkedList;
public class RPNCalculator {
public static void main(String[] args) {
RPNCalculator calc = new RPNCalculator();
calc.startInteractiveMode();
}
protected Scanner scanInput;
public LinkedList<Double> dataStack;
protected boolean interactiveModeEnabled;
public RPNCalculator() {
scanInput = new Scanner(System.in).useDelimiter("\s+");
dataStack = new LinkedList<Double>();
}
public String getCommand() {
return scanInput.next();
}
public boolean executeCommand(String command) {
Scanner str = new Scanner(command);
if (str.hasNextDouble()) {
dataStack.push(str.nextDouble());
return true;
} else {
System.out.format(" DEBUG: command: %s$%n", command);
if(command == "+") { ommitedOp("add"); return true; }else
if(command == "-") { ommitedOp("subtract"); return true; }else
if(command == "*") { ommitedOp("multiply"); return true; }else
if(command == "/") { ommitedOp("divide"); return true; }else
if(command == ".") { ommitedOp("print"); return true; }else
if(command == ".s") { ommitedOp("showStack");return true; }else
if(command == "exit"){ ommitedOp("exit"); return true; }else
{
System.out.println("The command does not exist.");
return false;
}
}
}
public void startInteractiveMode() {
interactiveModeEnabled = true;
while (interactiveModeEnabled) {
String command = getCommand();
executeCommand(command);
}
}
public void ommitedOp(String method){
System.out.println("Command exists!");
}
}
...我想我明白了。谢谢,堆栈溢出的类似问题!
问题在于我尝试如何使用 ==
运算符,它只比较指针而不是String
本身:https://stackoverflow.com/a/10535836/3397179
在 Java 中,必须使用
两个对象可以相等equals()
来比较String
之间的相等性。==
测试身份,这是一个不同的概念。但不相同;另一方面,如果两个对象相同,则暗示它们是相等的。
如果两个对象在物理上指向内存中的相同地址,则它们是相同的,而如果两个对象具有相同的值,则它们是相等的,如程序员在
equals()
方法中所定义的那样。通常,您更感兴趣的是确定两个对象是否相等。— 回答 五月 10 '12 在 14:11 由奥斯卡·洛佩斯
现在,让我们在发帖之前测试一下这个理论,以免自欺欺人,不必要地浪费别人的时间......证实。
<小时 />因此,解决方案是使用 command.equals("COMMAND NAME")
而不是 command == "COMMAND NAME"
,如下所示:
public boolean executeCommand(String command) {
if(command.equals("+")) { add(); return true; }else
if(command.equals("-")) { subtrair(); return true; }else
if(command.equals("*")) { multiplicar(); return true; }else
if(command.equals("/")) { dividir(); return true; }else
{
System.out.println("The command does not exist.");
return false;
}
}
command == "+" // always checks for reference and it will be never same.
而是使用以下一个
command.equals("=")