在Java中使用enum和switch语句

时间:2014-05-28 21:05:03

标签: java enums

我正在尝试编写计算器,但在使用任何类型Enum(+, - ,/,*)的操作进行测试并单击等于按钮时,程序将直接进入默认语句。我该如何解决这个问题?

public enum OpType {
Plus('+'),Minus('-'),Division('/'),Multiplication('X'),Void(' ');
private char operation;

private OpType(char operation) {
    setOperation(operation);
}

private OpType() {
    this(' ');
}

public static OpType getPlus() {
    return Plus;
}

public static OpType getVoid() {
    return Void;
}

public static OpType getMinus() {
    return Minus;
}

public static OpType getDivision() {
    return Division;
}

public static OpType getMultiplication() {
    return Multiplication;
}

 public void setOperation(char operation) {
    this.operation = operation;
}

public char getOperation() {
    return operation;
}


private double dblResult;
private OpType opType;

/**
 * Creates new form Calculator1
 */

public Calculator1(double dblResult, OpType opType) {
   setDblResult(dblResult);
    setOpType(opType);

}



public OpType getOpType() {
    return opType;
}

public void setOpType(OpType opType) {
    this.opType = opType;
}


public Calculator1() {
    initComponents();
    opType = OpType.Void;
}

public double getDblResult() {
    return dblResult;
}

public void setDblResult(double dblResult) {
    this.dblResult = dblResult;
}

// ...

private void oneActionPerformed(java.awt.event.ActionEvent evt) {                                    
    if("0".equals(textField.getText())){
        textField.setText("1");
    }
    else{
    textField.setText(String.format(textField.getText() + "1"));
    }
}                                   

private void twoActionPerformed(java.awt.event.ActionEvent evt) {                                    
    if("0".equals(textField.getText())){
        textField.setText("2");
    }
    else{
    textField.setText(String.format(textField.getText() + "2"));
    }

}                                   

private void threeActionPerformed(java.awt.event.ActionEvent evt) {                                      
    if("0".equals(textField.getText())){
        textField.setText("3");
    }
    else{
    textField.setText(String.format(textField.getText() + "3"));
    }
}                                     

private void fourActionPerformed(java.awt.event.ActionEvent evt) {                                     
    if("0".equals(textField.getText())){
        textField.setText("4");
    }
    else{
    textField.setText(String.format(textField.getText() + "4"));
    }
}                                    

private void fiveActionPerformed(java.awt.event.ActionEvent evt) {                                     
    if("0".equals(textField.getText())){
        textField.setText("5");
    }
    else{
    textField.setText(String.format(textField.getText() + "5"));
    }
}                                    

private void sixActionPerformed(java.awt.event.ActionEvent evt) {                                    
    if("0".equals(textField.getText())){
        textField.setText("6");
    }
    else{
    textField.setText(String.format(textField.getText() + "6"));
    }
}                                   

private void sevenActionPerformed(java.awt.event.ActionEvent evt) {                                      
    if("0".equals(textField.getText())){
        textField.setText("7");
    }
    else{
    textField.setText(String.format(textField.getText() + "7"));
    }
}                                     

private void eightActionPerformed(java.awt.event.ActionEvent evt) {                                      
    if("0".equals(textField.getText())){
        textField.setText("8");
    }
    else{
    textField.setText(String.format(textField.getText() + "8"));
    }
}                                     

private void nineActionPerformed(java.awt.event.ActionEvent evt) {                                     
    if("0".equals(textField.getText())){
        textField.setText("9");
    }
    else{
    textField.setText(String.format(textField.getText() + "9"));
    }
}                                    

private void zeroActionPerformed(java.awt.event.ActionEvent evt) {                                     
    if("0".equals(textField.getText())){
        textField.setText("0");
    }
    else{
    textField.setText(String.format(textField.getText() + "0"));
    }
}                                    

private void plusActionPerformed(java.awt.event.ActionEvent evt) {                                     
    dblResult = Double.parseDouble(textField.getText());
    OpType op1 = OpType.Plus;
    textField.setText(String.format("%s",op1.getOperation()));
}                                    

private void minusActionPerformed(java.awt.event.ActionEvent evt) {                                      
    dblResult = Double.parseDouble(textField.getText());
    OpType op2 = OpType.Minus;
    textField.setText(String.format("%s",op2.getOperation()));
}                                     

private void multiplyActionPerformed(java.awt.event.ActionEvent evt) {                                         
    dblResult = Double.parseDouble(textField.getText());
    OpType op3 = OpType.Multiplication;
    textField.setText(String.format("%s",op3.getOperation()));
}                                        

private void divideActionPerformed(java.awt.event.ActionEvent evt) {                                       
    dblResult = Double.parseDouble(textField.getText());
    OpType op4 = OpType.Division;
    textField.setText(String.format("%s",op4.getOperation()));
}                                      

private void decimalActionPerformed(java.awt.event.ActionEvent evt) {                                        
    String dot = ".";
    textField.setText(String.format(textField.getText() + "%s", dot));
}                                       

private void equalsActionPerformed(java.awt.event.ActionEvent evt) {                                       
    double result;
    switch(opType){
        case Plus: 
            result = dblResult + Double.parseDouble(textField.getText());
            textField.setText(String.format("%f",result));
            break;
        case Minus:
            result = dblResult + Double.parseDouble(textField.getText());
            textField.setText(String.format("%f",result));
            break;
        case Division:
            result = dblResult + Double.parseDouble(textField.getText());
            textField.setText(String.format("%f",result));
            break;
        case Multiplication:
            result = dblResult + Double.parseDouble(textField.getText());
            textField.setText(String.format("%f",result));
            break;
        default:
            textField.setText("error");
    }
}

0 个答案:

没有答案