开发者

Do i need to use a method or can I simply pass an operation when it is changed?

Is it possible for me to pass the operation string back from when a +, - or * operation occurs so that it saves it as operation and then I can move on to when I press = or is this simply impossible?

Code part 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 = intVa开发者_高级运维lueNeg * (-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"); 
        }                      
    }
}

Code part 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);


To check strings for equality, you should always use the equals() method. Taking a piece of your code, it would look like this:

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

If flip can possibly be null, you could re-arrange the test to look like this:

if ("-".equals(flip))

Java has an optimization, where it reuses literal string values. So if you write something like this, the two variables point to the same physical object, and == will return true.

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

However, if you're reading a string value from a GUI (which it looks like you're doing), this is not a literal value, and is not optimized in that way. It's a good habit to always use equals() to check for equality of objects, and only use == for primitive values.


Always use the Strategy design pattern for operations like this below.

//StrategyExample test application

public class StrategyExample { public static void main(String[] args) { Context context; // Three contexts following different strategies context = new Context(new ConcreteStrategyAdd()); int resultA = context.executeStrategy(3, 4); context = new Context(new ConcreteStrategySubtract()); int resultB = context.executeStrategy(3, 4); context = new Context(new ConcreteStrategyMultiply()); int resultC = context.executeStrategy(3, 4); } }

//The classes that implement a concrete strategy should implement this // The context class uses this to call the concrete strategy public interface Strategy { int execute(int a, int b); }

//Implements the algorithm using the strategy interface public class ConcreteStrategyAdd implements Strategy { public int execute(int a, int b) { System.out.println("Called ConcreteStrategyA's execute"); return a + b; // Do an addition with a and b } }

public class ConcreteStrategySubtract implements Strategy { public int execute(int a, int b) { System.out.println("Called ConcreteStrategyB's execute()"); return a - b; // Do a subtraction with a and b } }

public class ConcreteStrategyMultiply implements Strategy { public int execute(int a, int b) { System.out.println("Called ConcreteStrategyC's execute()"); return a * b; // Do a multiplication with a and b } }

//Configured with a ConcreteStrategy object and maintains a reference to a Strategy object 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);
}

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜