为什么这个java应用程序挂起/冻结?

时间:2017-11-04 02:54:27

标签: java

我目前正在编写一个代码,用于计算第n个项的PI和E.我在TermsJTextField中输入一个数字,选择PI或E单选按钮(一次只能激活一个按钮),按计算,它应该在适当的位置显示答案。但是,当我按下计算时,应用程序会挂起,并且没有任何按钮响应,甚至是x按钮。答案永远不会显示出来。

这是代码。我已经缩小到让我头痛的部分:

    private void CalculateJButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                 
    final double MAX_VALUE = 10000; //Max value
    double Start, End;              //Star and end time
    BigDecimal result = new BigDecimal ("0");  // Constants for result
    BigDecimal Error = new BigDecimal ("0");   // And Error
    DecimalFormat integerFormatter = new DecimalFormat("#0.");
    Start = System.currentTimeMillis();
    int Terms;
    int count = 1;

    boolean PIchecked = PiJRadioButton.isSelected();
    boolean Echecked = EJRadioButton.isSelected();
    double PI = 0;
    double E = 1;
    try
    {
        Terms = Integer.parseInt(TermsJTextField.getText());
        if ((Terms <= 2) || (Terms >= 10000)) // This checks for the number of terms
        {
            throw new NumberFormatException();
        }
        else
        {
            if (PIchecked) // If Pi butoon is selected, do the following calculation
            {
                for (int i =1 ; 1 <= (Terms);i++)
                {
                    count++;
                    result = result.add(new BigDecimal (Math.pow((-1.0),count)/(2*i-1)));
                }
            EJRadioButton.setSelected(false);
            result = result.multiply(new BigDecimal (4.0));
            Error = new BigDecimal(Math.abs(PI-result.doubleValue())/PI * 100.0);
            }
        else if (Echecked) // This calculates nth term for E
        {   
            result = new BigDecimal("0");
                long factorial = 1L;

                for (int i = 1; i < Terms ; i++)
                {
                    factorial *= i;
                    result = result.add(new BigDecimal(1.0/factorial));
                }
                result = result.add(new BigDecimal(1L));
                Error = new BigDecimal(Math.abs(E-result.doubleValue())/E * 100.0);
            PiJRadioButton.setSelected(false);

        }

                   End = System.currentTimeMillis(); //Time in ms to calculate the answer

    //Output
    DecimalFormat Number = new DecimalFormat("#####0.##");
    if (PIchecked)
    {
        EJTextField.setText("");
        PIJTextField.setText(String.valueOf(result));
        ErrorJTextField.setText(String.valueOf(Error + "%"));
    }
    else
    {
        PIJTextField.setText("");
        EJTextField.setText(String.valueOf(result));
        ErrorJTextField.setText(String.valueOf(Error + "%"));
    }
    PrintJButton.setEnabled(true);
    PrintJMenuItem.setEnabled(true);
    TimeJTextField.setText(String.valueOf(End-Start));
    }
    }


    catch(NumberFormatException exp)

      {

          Object ERROR_MESSAGE = null;
                JOptionPane.showMessageDialog(null, "Don't be silly; Enter a vaule between 2 and 10000", 
              "Input Error", JOptionPane.ERROR_MESSAGE);
                 TermsJTextField.selectAll();
                 TermsJTextField.setText("");
                 TermsJTextField.requestFocus();
            }          

}

1 个答案:

答案 0 :(得分:2)

您的for循环的终止条件似乎永远不会完成,如下所示:

for (int i =1 ; 1 <= (Terms);i++)

将其切换为以下内容应将其修复(将1更改为i):

for (int i = 1; i <= Terms; i++)