0

我尝试从屏幕上读取文本并将其更改为双倍并崩溃

public void equesionOperation(int signNum1) {

        S_numInTV=TV_calcScreen.getText().toString();
        S_numUp=TV_calcUp.getText().toString();

        D_numIn=Double.parseDouble(S_numInTV);
        D_numToCalc=Double.parseDouble(S_numUp);

        switch (signNum1){
        case 1: D_sum=D_numIn+D_numToCalc;break;
        case 2: D_sum=D_numIn-D_numToCalc;break;
        case 3: D_sum=D_numIn*D_numToCalc;break;
        case 4: D_sum=D_numToCalc/D_numIn;break;
        case 5: D_sum=Math.pow(D_numToCalc, D_numIn);break;
        default: break;
        }
        S_sum=(""+D_numToCalc+"  "+D_numIn);
    }
4

2 回答 2

0

这可能有助于找到崩溃的原因。将第 3 行和第 4 行代码替换为:

try {
   D_numIn=Double.parseDouble(S_numInTV);
} catch (NumberFormatException nfe) {
   System.out.printf("Attempted to parse a double, but found: '%s'%n", S_numInTV);
}
try {
D_numToCalc=Double.parseDouble(S_numUp);
} catch (NumberFormatException nfe) {
   System.out.printf("Attempted to parse a double, but found: '%s'%n", S_numUp);
}
于 2012-08-26T08:55:35.453 回答
0
 S_numInTV=TV_calcScreen.getText().toString();
 S_numUp=TV_calcUp.getText().toString();

这些字符串值是文字还是数字?

String str1 = "100.476";
Double dObj2 = Double.valueOf(str1);
System.out.println(dObj2);

上面的代码不会出现任何异常但是

String str1 = "ss";
    Double dObj2 = Double.valueOf(str1);
    System.out.println(dObj2);

上面的代码会发生numberfomateexception

因此,在将字符串转换为双精度之前,您必须确保输入的是哪个字符串;

于 2012-08-26T09:04:51.727 回答