输入不匹配异常

ldxq2e6h  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(391)

当我输入整数,如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);
    }
    }


xqk2d5yq

xqk2d5yq1#

系统的区域设置不支持将点作为小数点分隔符。使用支持点作为分隔符的区域设置,例如。 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

相关问题