gpt4 book ai didi

java - 富="bar"; if(foo == "bar"){ doX(); }否则{ "But this ALWAYS runs"} 为什么?

转载 作者:行者123 更新时间:2023-11-30 02:57:13 26 4
gpt4 key购买 nike

我的类实现了一个非常简单的 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 between Strings. == 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com