你怎么围绕这个

时间:2014-03-10 14:49:12

标签: java string exception double

我正在从事高中数字转换器项目,这是为了“长度” 我的两个问题是:

  1. 如何将结果舍入为0.00值?
  2. 如何使异常允许十进制输入,如果数字不是整数,则显示错误消息。
  3. 这是我的代码:

    double a,a1,a2,a3,a4,a5,a6,a7;  
    double c1 = (double) (0.00062);  
    double c2 = (double) (39.3701);  
    double c3 = (double) (0.001);  
    double c4 = (double) (3.28084);  
    double c5 = (double) (1.09361);
    String b = "Invalid user input";  
    try {
    
    a = Integer.parseInt(tl.getText());    
    a1 = (a*1000);  
    a2 = (a*100);  
    a3 = (a*c1);  
    a4 = (a*c2);  
    a5 = (a*c3);  
    a6 = (a*c4);  
    a7 = (a*c5);  
    
    tl1.setText(String.valueOf(a1));  
    tl2.setText(String.valueOf(a2));  
    tl3.setText(String.valueOf(a3));  
    tl4.setText(String.valueOf(a4));  
    tl5.setText(String.valueOf(a5));  
    tl6.setText(String.valueOf(a6));  
    tl7.setText(String.valueOf(a7));
    
    
    }
    catch (Exception z) // <<<< THIS T_T  
    {  
     JOptionPane.showMessageDialog(rootPane, b);  
    }
    

2 个答案:

答案 0 :(得分:0)

要将int舍入到最近的100,只需执行:

Math.round(someNumber/100)*100

答案 1 :(得分:0)

您正在尝试将int解析的值分配给double变量,这就是导致错误的原因。 您应该将Integer.parseInt更改为Double.parseDouble