java - 我是否需要使用一种方法,或者我可以在更改时简单地传递一个操作吗?

标签 java methods operation

我是否可以在发生 +-* 操作时将操作字符串传递回来,以便保存它作为操作,然后我可以继续按 = 或这根本不可能?

代码第 1 部分:

public void actionPerformed(ActionEvent calculate)
    {
    JButton operand = (JButton) calculate.getSource();
    String flip = operand.getLabel();
    String operation = "";
    System.out.println(operation);
    String value1 = (box1.getText());
    String value2 = (box2.getText());
    box1.setText(box1.getText() + operand.getLabel());

    if (flip.equals("C"))
        {
        box2.setText("");
        box1.setText("");
        }

    if (flip.equals("!"))
        {
        int intValueNeg = Integer.parseInt(value1);
        int negateIntValue = intValueNeg * (-1);
        String negativeInt = Integer.toString(negateIntValue);
        box1.setText(negativeInt);
        }

    if (flip.equals("+")) 
        {
        box2.setText(value1);
        box1.setText("");
        operation = operand.getLabel();
        }

    if (flip.equals("-"))    
        {
        box2.setText(value1);
        box1.setText("");
        operation = operand.getLabel();
        }

    if (flip.equals("*"))
        {
        box2.setText(value1);
        box1.setText("");
        operation = operand.getLabel();
        }

    if (flip.equals("=") && operation.equals("+"))
        {
        int intValue1 = Integer.parseInt(value1);
        int intValue2 = Integer.parseInt(value2); 
        int totalValue = intValue1 + intValue2;
        String totalResult = Integer.toString(totalValue);
        box1.setText(totalResult);
        box2.setText("0");
        } 

    if (flip.equals("=") && operation.equals("-"))
        {
        int intValue1 = Integer.parseInt(value1);
        int intValue2 = Integer.parseInt(value2); 
        int totalValue = intValue2 - intValue1;
        String totalResult = Integer.toString(totalValue);
        box1.setText(totalResult);
        box2.setText("0"); 
        } 

    if (flip.equals("=") && operation.equals("*"))
        {
        int intValue1 = Integer.parseInt(value1);
        int intValue2 = Integer.parseInt(value2); 
        int totalValue = intValue1 * intValue2;
        String totalResult = Integer.toString(totalValue);
        box1.setText(totalResult);
        box2.setText("0"); 
        }                      
    }
}

代码第 2 部分:

box2 = new JTextField (10);
b.add(box2);

Blackbelt.add(a, BorderLayout.NORTH);
Blackbelt.add(c, BorderLayout.CENTER);
Blackbelt.add(b, BorderLayout.SOUTH);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

最佳答案

要检查字符串是否相等,您应该始终使用 equals() 方法。取一段代码,它看起来像这样:

if (flip.equals("-"))
{
    box2.setText(value1);
    box1.setText("");
    operation = "-";
}

如果 flip 可能为 null,您可以重新安排测试,如下所示:

if ("-".equals(flip))

Java 有一个优化,它重用文字字符串值。所以如果你写这样的东西,两个变量指向同一个物理对象,==将返回true。

String a = "foo";
String b = "foo";

但是,如果您从 GUI 读取字符串值(看起来您正在这样做),则这不是文字值,并且没有以这种方式进行优化。 始终使用equals()来检查对象是否相等,并且仅使用==来检查原始值是一个好习惯。

关于java - 我是否需要使用一种方法,或者我可以在更改时简单地传递一个操作吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5917396/

相关文章:

java - 如何允许用户从 JTextArea 复制、剪切和粘贴选定的单词?

java - 调用低于 2 度或更多度的函数

java - 将对象添加到 Documentum DfCheckOutOperation 时出现异常

regex - Emacs : Search and replace operations

java - Swift 中的按位移位与 Java 不同

java - 我想从文件中的一行读取数据。在 java

java - 为什么这两个 for 循环给出不同的结果?

java - @OneToMany Map 默默删除 Map 的键

python - 列出一个类的方法并动态调用该类的方法

objective-c - 关闭文档时在 document.m 中调用的方法