Java计算器减去错误

时间:2014-06-10 07:58:06

标签: java

我已经写了这个计算器代码。

但是,在运行时,如果我使用减号按钮,它会变为加号 我曾尝试删除该加/减按钮的代码,但问题仍然存在。

我已经尝试了所有内容,我发现唯一有用的是“ - ”字符串没有注册。

代码:

public class Calculator extends javax.swing.JFrame {
    static String OP = "",
              operations = "[+|-|x|/|0|-|.]",
              anw = "noInputYet";
    static float result,
             firstNumber = 0, 
             secondNumber = 0;
    static double prNumber = 0 ;
    static int numberClick = 0;



    private void bt1ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // Button 1 

        if (calScreen.getText().matches(operations)) {
            calScreen.setText("1");}

        else if (result != 0) {
           calScreen.setText("1");
           result = 0;}

        else {
            calScreen.setText(calScreen.getText() + "1");
        }
    }                                   

    private void bt2ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // button 2 
        if (calScreen.getText().matches(operations)) {
            calScreen.setText("2");}

        else if (result != 0) {
           calScreen.setText("2");
           result = 0;}

        else {
            calScreen.setText(calScreen.getText() + "2");
        }
    }                                   

    private void bt3ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // button 3 
        if (calScreen.getText().matches(operations)) {
            calScreen.setText("3");}

        else if (result != 0) {
           calScreen.setText("3");
           result = 0;}

        else {
            calScreen.setText(calScreen.getText() + "3");
        }
    }                                   

    private void bt9ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // button 9
        if (calScreen.getText().matches(operations)) {
            calScreen.setText("9");}

        else if (result != 0) {
           calScreen.setText("9");
           result = 0;}

        else {
            calScreen.setText(calScreen.getText() + "9");
        }
    }                                   

    private void bt8ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // button 8
        if (calScreen.getText().matches(operations)) {
            calScreen.setText("8");}

        else if (result != 0) {
           calScreen.setText("8");
           result = 0;}

        else {
            calScreen.setText(calScreen.getText() + "8");
        }
    }                                   

    private void bt7ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // button 7
        if (calScreen.getText().matches(operations)) {
            calScreen.setText("7");}

        else if (result != 0) {
           calScreen.setText("7");
           result = 0;}

        else {
            calScreen.setText(calScreen.getText() + "7");
        }
    }                                   

    private void bt4ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // button 4
        if (calScreen.getText().matches(operations)) {
            calScreen.setText("4");}

        else if (result != 0) {
           calScreen.setText("4");
           result = 0;}

        else {
            calScreen.setText(calScreen.getText() + "4");
        }
    }                                   

    private void bt5ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // button 5
        if (calScreen.getText().matches(operations)) {
            calScreen.setText("5");}

        else if (result != 0) {
           calScreen.setText("5");
           result = 0;}

        else {
            calScreen.setText(calScreen.getText() + "5");
        }
    }                                   

    private void bt6ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // button 6
        if (calScreen.getText().matches(operations)) {
            calScreen.setText("6");}

        else if (result != 0) {
           calScreen.setText("6");
           result = 0;}

        else {
            calScreen.setText(calScreen.getText() + "6");
        }
    }                                   

    private void btDotActionPerformed(java.awt.event.ActionEvent evt) {                                      
        // button dot 
        if (calScreen.getText().matches(operations)) {
            calScreen.setText("0.");}

        else if (result != 0) {
           calScreen.setText("0.");
           result = 0;}

        else {
            calScreen.setText(calScreen.getText() + ".");
        }
    }                                     

    private void bt0ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        //  button 0
        if (calScreen.getText().matches(operations)) {
            calScreen.setText("0");}

        else if (result != 0) {
           calScreen.setText("0");
           result = 0;}

        else {
            calScreen.setText(calScreen.getText() + "0");
        }

    }                                   

    private void btEqualActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // Result
        secondNumber = Float.parseFloat(calScreen.getText());
        switch(OP){
            case "+" :{
                result = firstNumber +  secondNumber;break;
            }
            case "-":{
                result = firstNumber - secondNumber;break;
            }

            case "*":{
                result = firstNumber * secondNumber;break;
            }
            case "/":{
                result = firstNumber / secondNumber;break;
            }
            default :{
                result = 0 ;break;
            }
        }
        if (result == (int)result){
            calScreen.setText(Integer.toString((int)result));
            anw = Integer.toString((int)result); 
        }//If result is integer
        else {calScreen.setText(Float.toString(result));
              anw = Float.toString(result);
              }// If result is flaot 
        firstNumber= 0;
        secondNumber = 0;
        numberClick = 0;

    }                                       

    private void btPlusActionPerformed(java.awt.event.ActionEvent evt) {                                       
        //Plus button
        firstNumber = Float.parseFloat(calScreen.getText());
        OP = "+";
        numberClick += 1;
        if (!calScreen.getText().equals("+") && numberClick == 2){
            btEqual.doClick();
        }
        else {
            calScreen.setText("+");
        }


    }                                      

    private void btMinusActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // minus button
        firstNumber = Float.parseFloat(calScreen.getText());
        OP = "-";
        numberClick += 1;
        if (!calScreen.getText().equals("-") && numberClick == 2){
            btEqual.doClick();
        }
        else {
            calScreen.setText("-");
        }
    }                                       

    private void btMultiActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // Multiply
        firstNumber = Float.parseFloat(calScreen.getText());
        OP = "*";
        numberClick += 1;
        if (!calScreen.getText().equals("x") && numberClick == 2){
            btEqual.doClick();
        }
        else {
            calScreen.setText("x");
        }
    }                                       

    private void btDivideActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // divide button
        firstNumber = Float.parseFloat(calScreen.getText());
        OP = "/";
        numberClick += 1;
        if (!calScreen.getText().equals("/") && numberClick == 2){
            btEqual.doClick();
        }
        else {
            calScreen.setText("/");
        }    
    }                                        

    private void btAnsActionPerformed(java.awt.event.ActionEvent evt) {                                      
        // Answer button
        if (!anw.equals("noInputYet")){
        calScreen.setText(anw);
        }
    }                                     

    private void btSignActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // button Change sign

    }                                      

    private void btPiActionPerformed(java.awt.event.ActionEvent evt) {                                     
        // precent button 

            prNumber = Double.parseDouble(calScreen.getText())/100;
            if (prNumber == (int)prNumber){
            calScreen.setText(Integer.toString((int)prNumber));
            anw = Integer.toString((int)prNumber); 
            result = (int)prNumber; 
              }//prNumber is integer
            else {calScreen.setText(Double.toString(prNumber));
              anw = Double.toString(prNumber);
              result = Float.parseFloat(Double.toString(prNumber));
              }// If prNumber is flaot


    }                                    

    private void ButtonAllClearActionPerformed(java.awt.event.ActionEvent evt) {                                               
        // ALL clear button
        calScreen.setText("0");
        firstNumber = 0;
        secondNumber = 0;
        result = 0;
        numberClick = 0; 
        anw = "noInputYet";
    }                                              

    private void btPwOf2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // button power of 2 
        result = Float.parseFloat(calScreen.getText()) * Float.parseFloat(calScreen.getText());
        if (result == (int)result){
            calScreen.setText(Integer.toString((int)result));
            anw = Integer.toString((int)result); 
              }//prNumber is integer
            else {calScreen.setText(Float.toString(result));
              anw = Float.toString(result);
              }// If prNumber is flaot
    }                                       

    private void calScreenActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // Screen of Calculator

    }                                         

2 个答案:

答案 0 :(得分:0)

查看您的btMinusActionPerformed方法:

private void btMinusActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // minus button
    firstNumber = Float.parseFloat(calScreen.getText());
    OP = "-";
    numberClick += 1;
    if (!calScreen.getText().equals("-") && numberClick == 2){
        btEqual.doClick();
    }
    else {
        calScreen.setText("-"); // <- What does this do?
    }
}

然后看一下btEqualActionPerformed方法:

private void btEqualActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // Result
    // What is getText() going to return if you pressed the minus button?
    secondNumber = Float.parseFloat(calScreen.getText());
    { ... } // Omitted for brevity
}

通过逻辑关注,您会注意到当您点击1-2按钮时,您会看到1和{{ 1}}。您已经将操作数存储在-2中,因此您要做的是OP,它实际上等于1 - (-2)。您的问题出在3 - 您需要修改代码。


此外,您的btMinusActionPerformed正则表达式定义不正确。您尚未转义operations-,因此它们无法以您期望的方式工作。

.

这应该使你的正则表达式以你想要的方式工作。

另一种解决方案是删除:

operations = "[+|\\-|x|/|0|\\.]",
//               ^         ^
// Note the escape characters at these two locations

calScreen.setText("-"); 方法(以及其他三个运算符的方法中的相应行),这意味着不需要正则表达式。

答案 1 :(得分:0)

在此处设置文本字段中的减号。

private void btMinusActionPerformed(java.awt.event.ActionEvent evt) { // minus button 
    firstNumber = Float.parseFloat(calScreen.getText()); 
    OP = "-"; 
    numberClick += 1; 
    if (!calScreen.getText().equals("-") && numberClick == 2){ 
      btEqual.doClick(); 
    } else { 
      calScreen.setText("-"); 
    } 
}  

以后如果你打电话给calScreen.getText());,你会得到负值。 所以删除

calScreen.setText("-"); 

它应该有用。