输入不匹配异常
Input Mismatch Exception
当我输入 5、7、14 等整数时,一切正常。但是当我输入 例如 7.5 时,我得到一个错误。哪里错了?
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextDouble(Scanner.java:2564)
at Exercise_11.main(Exercise_11.java:8)
代码如下:
import java.util.Scanner;
public class Exercise_11 {
public static void main(String[] args) {
Scanner radius = new Scanner(System.in);
System.out.print("Please input the radius of circle: ");
double r= radius.nextDouble();
double l= 2*Math.PI*r;
double s= Math.PI*r*r;
System.out.println("Length = " + l);
System.out.println("Area = " + s);
}
}
您系统的区域设置不支持点作为小数点分隔符。使用支持点作为分隔符的语言环境,例如Locale.ENGLISH
.
Scanner radius = new Scanner(System.in).useLocale(Locale.ENGLISH);
样本运行:
Please input the radius of circle: 7.5
Length = 47.12388980384689
Area = 176.71458676442586
当我输入 5、7、14 等整数时,一切正常。但是当我输入 例如 7.5 时,我得到一个错误。哪里错了?
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextDouble(Scanner.java:2564)
at Exercise_11.main(Exercise_11.java:8)
代码如下:
import java.util.Scanner;
public class Exercise_11 {
public static void main(String[] args) {
Scanner radius = new Scanner(System.in);
System.out.print("Please input the radius of circle: ");
double r= radius.nextDouble();
double l= 2*Math.PI*r;
double s= Math.PI*r*r;
System.out.println("Length = " + l);
System.out.println("Area = " + s);
}
}
您系统的区域设置不支持点作为小数点分隔符。使用支持点作为分隔符的语言环境,例如Locale.ENGLISH
.
Scanner radius = new Scanner(System.in).useLocale(Locale.ENGLISH);
样本运行:
Please input the radius of circle: 7.5
Length = 47.12388980384689
Area = 176.71458676442586