当输出给出“ NaN”时,如何修复我的计算代码?

时间:2019-01-28 15:21:42

标签: java jframe

我正在编码SUVAT计算器(物理公式)。它仅需要4个输入中的3个。当我输入3个值时,输出将显示NaN。

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    Output Ans = new Output();
    int n=0;//Initailize the n variable      

    {if (NumDis.getText().isEmpty()){//To count the number of input           
        n = n+1;
        }
    }
    {if (NumIni.getText().isEmpty()){           
        n = n+1;
        }
    }
    {if (NumFin.getText().isEmpty()){           
        n = n+1;
        }
    }       
    {if (NumTime.getText().isEmpty()){
        n = n+1;
    }
}
    Acceleration CallAccCal = new Acceleration();
    if(n == 1){//To Check if the number of input is 3
        CallAccCal.AccCal();//If yes, call the AccCal to calculate
        Ans.setVisible(true);//To make the Output class visible       
        }
else{
        Warning Warning = new Warning();
        Warning.setVisible(true);//If no, make the Warning class visible
}

}
double AccCal(){

    double Ini=0,Dis=0, Fin=0, Time=0, ans=0;

    if(!NumIni.getText().isEmpty()){
        Ini = Double.parseDouble(NumIni.getText());
            if(UUnit.isSelected()){ //To change the Unit
            Ini = Ini*1000;
            }
    }
    else{
        Ini=0;
    }
    if(!NumDis.getText().isEmpty()){
        Dis = Double.parseDouble(NumDis.getText());
            if(SUnit.isSelected()){
            Dis = Dis*1000;
    }
    }
    else{
        Dis=0;
    }
    if(!NumFin.getText().isEmpty()){
        Fin = Double.parseDouble(NumFin.getText());
        if(VUnit.isSelected()){
            Fin = Fin*1000;
            }
    }
    else{
        Fin=0;
    }
    if(!NumTime.getText().isEmpty()){
        Time = Double.parseDouble(NumTime.getText());
        if(TUnit.isSelected()){
            Time = Time*60;
            }
    }
    else{
        Time=0;
    }
        {
      if (NumDis.getText().isEmpty()){ 
          ans = (Fin-Ini)/Time;           
        }
      else if (NumFin.getText().isEmpty()){
          ans = 2*(Dis-(Ini*Time))/(Time*Time);
        }
      else if (NumIni.getText().isEmpty()){
          ans = 2*((Fin*Time)-Dis)/(Time*Time);
        }
      else if (NumTime.getText().isEmpty()){
          ans = ((Fin*Fin)-(Ini*Ini))/(2*Dis);
    }
        }
    return ans;

}

String format(double Ans){

    return String.format ("%.1f", Ans);}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {   

    Acceleration AccAns = new Acceleration();
    double Answer = AccAns.AccCal();
    TextFieldAns.setText(format(Answer));}

1 个答案:

答案 0 :(得分:0)

要在返回它之前将其转换为字符串吗?

如果是这样的话。