有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java foo=“bar”;如果(foo==“bar”){doX();}否则{“但这总是运行”}为什么?

我的类实现了一个非常简单的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!");
    }
}

共 (1) 个答案

  1. # 1 楼答案

    command == "+"   // always checks for reference and it will be never same.
    

    而是使用下面的一个

    command.equals("=")