0

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

代码第 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);
4

2 回答 2

1

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

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

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

if ("-".equals(flip))

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

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

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

于 2011-05-06T22:30:15.023 回答
0

始终使用策略设计模式进行如下操作。

//StrategyExample 测试应用

公共类 StrategyExample { public static void main(String[] args) { 上下文上下文;// 遵循不同策略的三个上下文 context = new Context(new ConcreteStrategyAdd()); int resultA = context.executeStrategy(3, 4); 上下文 = 新上下文(新的 ConcreteStrategySubtract()); int resultB = context.executeStrategy(3, 4); 上下文 = 新上下文(新的具体策略乘法());int resultC = context.executeStrategy(3, 4); } }

//实现具体策略的类应该实现this //上下文类使用this调用具体策略 public interface Strategy { int execute(int a, int b); }

//使用策略接口实现算法 public class ConcreteStrategyAdd implements Strategy { public int execute(int a, int b) { System.out.println("Called ConcreteStrategyA's execute"); 返回 a + b; // 用 a 和 b 做加法 } }

公共类 ConcreteStrategySubtract 实现策略 { public int execute(int a, int b) { System.out.println("Called ConcreteStrategyB's execute()"); 返回 a - b; // 用 a 和 b 做减法 } }

公共类 ConcreteStrategyMultiply 实现策略 { public int execute(int a, int b) { System.out.println("Called ConcreteStrategyC's execute()"); 返回 a * b; // 做 a 和 b 的乘法 } }

//配置一个ConcreteStrategy对象,维护一个Strategy对象的引用 public class Context { private Strategy strategy;

// Constructor
public Context(Strategy strategy) {
    this.strategy = strategy;
}

public int executeStrategy(int a, int b) {
    return strategy.execute(a, b);
}

}

于 2011-05-07T03:21:37.120 回答