2017-11-04 101 views
-1

我目前正在编写计算PI和E到第n项的代码。我在TermsJTextField中输入一个数字,选择PI或E单选按钮(一次只能有一个按钮处于活动状态),按下计算,它应该显示适当的答案。然而,当我按下calculate时,应用程序会处于挂起状态,并且没有任何按钮会响应,即使是x按钮。答案从不显示。这个Java应用程序为什么挂起/冻结?

这是代码。我已经收窄到让我头疼的部分:

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(); 
      }   

} 
+2

你用调试器或分析器找出程序花费其大部分时间?无响应的部分可能只是试图在应用程序线程上做太多。 – Carcigenicate

+0

还要仔细看看你的循环......尤其是在完成条件下 – IEE1394

+0

看来你的代码运行过多迭代可能是无限的,应该有一个中断或限制条件来停止执行 –

回答

2

看来,你的终止条件您的循环将永远不会完成,如下所示:

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

它切换到下面应该修复它(改变1i):

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

@Robert'1'已更改为'i',修复了终止条件。 –

+0

尝试过它,但现在它不承认适当的数字(3,15,4等) – sgy0003

+0

这是什么意思? –