堆栈溢出错误 - 可能是无限循环

时间:2015-09-16 13:37:20

标签: java

请原谅我的无知,因为我对编程很陌生。我目前正在尝试构建一个计算器项目,其中我需要一个Jframe类和一个Java类。 jframe包含所有接口,大部分计算都在java类上。我所做的是我在Java类上创建了一个方法,并尝试在Jframe类上调用它。

先谢谢欢呼! 这是Java Class的代码

public class Calculations extends cpCalcFrame {

    public double mathoperations()
    {
        String answer;
        secNum = Double.parseDouble(txtOutput.getText());
        switch (operation)
      {
         case "+":
             if(secNum == 0)
             {
                 msgMessage.showMessageDialog(this, "ERROR: Cannot add by zero", "Error Message", javax.swing.JOptionPane.ERROR_MESSAGE);
             }
             else
             {
            result= firstNum + secNum;
            answer = String.format("%.0f", result);
            txtOutput.setText(answer);
            break;
             }

         case "-":
             if(secNum == 0)
             {
                 msgMessage.showMessageDialog(this, "ERROR: Cannot subtract by zero", "Error Message", javax.swing.JOptionPane.ERROR_MESSAGE);
             }
             else
             {
            result= firstNum - secNum;
            answer = String.format("%.0f", result);
            txtOutput.setText(answer);           
            break;
             }

    case "*":
            if(secNum == 0)
             {
                 msgMessage.showMessageDialog(this, "ERROR: Cannot multiply by zero", "Error Message", javax.swing.JOptionPane.ERROR_MESSAGE);
             }
             else
             {
            result= firstNum * secNum;
            answer = String.format("%.0f", result);
            txtOutput.setText(answer);
            break;
             }

         case "/":
             if(secNum == 0)
             {
                 msgMessage.showMessageDialog(this, "ERROR: Cannot divide by zero", "Error Message", javax.swing.JOptionPane.ERROR_MESSAGE);
             }
             else
             {
            result= firstNum / secNum;
            answer = String.format("%.0f", result);
            txtOutput.setText(answer);

            break;
             }

        }
        return result;
    }
}

这是我的Jframe类代码:

public class cpCalcFrame extends javax.swing.JFrame {

    public double firstNum;
    public double secNum;
    public double result;
    public String operation;
    **Calculations b = new Calculations();**
    /**
     * Creates new form cpCalcFrame
     */
    public cpCalcFrame() {
        initComponents();
        this.setSize(330, 320);
    }          

    private void btn1ActionPerformed(java.awt.event.ActionEvent evt) {                                     
        String input;
        input = txtOutput.getText() + btn1.getText();
        txtOutput.setText(input);
    }                                    

    private void btn2ActionPerformed(java.awt.event.ActionEvent evt) {                                     
        String input;
        input = txtOutput.getText() + btn2.getText();
        txtOutput.setText(input);
    }                                    

    private void btn3ActionPerformed(java.awt.event.ActionEvent evt) {                                     
        String input;
        input = txtOutput.getText() + btn3.getText();
        txtOutput.setText(input);
    }                                    

    private void btn4ActionPerformed(java.awt.event.ActionEvent evt) {                                     
        String input;
        input = txtOutput.getText() + btn4.getText();
        txtOutput.setText(input);
    }                                    

    private void btn5ActionPerformed(java.awt.event.ActionEvent evt) {                                     
        String input;
        input = txtOutput.getText() + btn5.getText();
        txtOutput.setText(input);
    }                                    

    private void btn6ActionPerformed(java.awt.event.ActionEvent evt) {                                     
        String input;
        input = txtOutput.getText() + btn6.getText();
        txtOutput.setText(input);
    }                                    

    private void btn7ActionPerformed(java.awt.event.ActionEvent evt) {                                     
        String input;
        input = txtOutput.getText() + btn7.getText();
        txtOutput.setText(input);
    }                                    

    private void btn8ActionPerformed(java.awt.event.ActionEvent evt) {                                     
        String input;
        input = txtOutput.getText() + btn8.getText();
        txtOutput.setText(input);
    }                                    

    private void btn9ActionPerformed(java.awt.event.ActionEvent evt) {                                     
        String input;
        input = txtOutput.getText() + btn9.getText();
        txtOutput.setText(input);
    }                                    

    private void btn0ActionPerformed(java.awt.event.ActionEvent evt) {                                     
        String input;
        input = txtOutput.getText() + btn0.getText();
        txtOutput.setText(input);
    }                                    

    private void btndecActionPerformed(java.awt.event.ActionEvent evt) {                                       
        String input;
        input = txtOutput.getText() + btndec.getText();
        txtOutput.setText(input);
    }                                      

    private void btnCAActionPerformed(java.awt.event.ActionEvent evt) {                                      
        txtOutput.setText("");
        firstNum = 0;
        secNum = 0;
    }                                     

    private void btnCEActionPerformed(java.awt.event.ActionEvent evt) {                                      
        txtOutput.setText("");
    }                                     

    private void btnmultActionPerformed(java.awt.event.ActionEvent evt) {                                        
        firstNum = Double.parseDouble(txtOutput.getText());
        txtOutput.setText("");
        operation = "*";
    }                                       

    private void btndivActionPerformed(java.awt.event.ActionEvent evt) {                                       
        firstNum = Double.parseDouble(txtOutput.getText());
        txtOutput.setText("");
        operation = "/";
    }                                      

    private void btnaddActionPerformed(java.awt.event.ActionEvent evt) {                                       
        firstNum = Double.parseDouble(txtOutput.getText());
        txtOutput.setText("");
        operation = "+";
    }                                      

    private void btnminActionPerformed(java.awt.event.ActionEvent evt) {                                       
        firstNum = Double.parseDouble(txtOutput.getText());
        txtOutput.setText("");
        operation = "-";
    }                                      

    **private void btnequalActionPerformed(java.awt.event.ActionEvent evt) {                                         
        b.mathoperations();**
    }                                        

    private void btnexclam1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        if (txtOutput.getText().length() > 0) {
        txtOutput.setText(txtOutput.getText().substring(0, txtOutput.getText().length() - 1));
}
    }                                          

1 个答案:

答案 0 :(得分:0)

CalculationscpCalcFrame的子类,cpCalcFrame有一个字段,该字段使用 Calculations对象初始化:

Calculations b = new Calculations();

所以,当你第一次在其他地方打电话给new Calculations()时,它就是 将初始化超级类,调用new Calculations(),其中 将初始化超级类,调用new Calculations(),其中 将初始化超级类,调用new Calculations(),其中 将初始化超级类,调用new Calculations(),其中 将初始化超级类,调用new Calculations(),其中 将初始化超级类,调用new Calculations(),其中 将抛出StackOverflowError

解决方案:从extends cpCalcFrame移除Calculations,并在mathoperations()方法上传递任何所需的值。