import java.io.*;
import java.text.*;
//import corejava.*; // important--imports corejava package
class Usesconstants2
{ System.out.println
("Enter a number, I'll add two to it.");
double x; // the number we wish to read
try
{ InputStreamReader isr= new InputStreamReader (System.in);
BufferedReader br= new BufferedReader(isr);
String s = br.readLine ();
DecimalFormat df = new DecimalFormat("0.00;-0.0");//?
String sqf=df.format(-4.12355);
System.out.println (sqf);
Number n = df.parse(s);
System.out.println (n);
String sf=df.format(n);
System.out.println(s);
System.out.println(sf);
x = n.doubleValue();
System.out.println(x);
}
catch(IOException e)
{ x= 0;
}
catch (ParseException e)
{
System.out.println ("ass");
x= 0;
}
System.out.println(x + 2.22322);
}
}
为什么 我输入负数时解析就会出现异常,输入正数就不会?
import java.text.*;
//import corejava.*; // important--imports corejava package
class Usesconstants2
{ System.out.println
("Enter a number, I'll add two to it.");
double x; // the number we wish to read
try
{ InputStreamReader isr= new InputStreamReader (System.in);
BufferedReader br= new BufferedReader(isr);
String s = br.readLine ();
DecimalFormat df = new DecimalFormat("0.00;-0.0");//?
String sqf=df.format(-4.12355);
System.out.println (sqf);
Number n = df.parse(s);
System.out.println (n);
String sf=df.format(n);
System.out.println(s);
System.out.println(sf);
x = n.doubleValue();
System.out.println(x);
}
catch(IOException e)
{ x= 0;
}
catch (ParseException e)
{
System.out.println ("ass");
x= 0;
}
System.out.println(x + 2.22322);
}
}
为什么 我输入负数时解析就会出现异常,输入正数就不会?
如果有错,请帖Stacktrace
import java.io.*;
import java.text.*;
//import corejava.*; // important--imports corejava package
class Usesconstants2
{
Usesconstants2()
{ System.out.println("Enter a number, I'll add two to it.");
……
……
……
System.out.println(x+2.22322);
}
public static void main(String[] args)
{ new Usesconstants2();
}
}
可以执行啊,又学了一点知识
catch (ParseException e){
System.out.println ("ass");
x= 0;
}
如果解析出现异常,就会执行这段代码;我输入负数时就会执行,正数不会
-12.345
-4.12
-12.345
-12.345
-12.34
-12.345
-10.121780000000001