- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的类实现了一个非常简单的 RPN 计算器原型(prototype)。
以下构造不起作用。为什么?我在这里做错了什么?
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!");
}
}
最佳答案
...我想我明白了。谢谢,Stack Overflow 的类似问题!
问题在于我如何尝试使用 ==
运算符,该运算符仅比较指针而不比较 String
本身:https://stackoverflow.com/a/10535836/3397179
In Java, you must use
equals()
for comparing equality betweenString
s.==
tests for identity, a different concept.Two objects can be equal but not identical; on the other hand if two objects are identical it's implied that they're equal.
Two objects are identical if they physically point to the same address in memory, whereas two objects are equal if they have the same value, as defined by the programmer in the
equals()
method. In general, you're more interested in finding out if two objects are equal.— answered May 10 '12 at 14:11 by Óscar López
现在,我们在发帖之前测试一下这个理论,以免出洋相,浪费别人不必要的时间……确认了。
<小时/>因此,解决方案是使用 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;
}
}
关于java - 富="bar"; if(foo == "bar"){ doX(); }否则{ "But this ALWAYS runs"} 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36887507/
我是一名优秀的程序员,十分优秀!