【问题标题】:Exception in thread "main" java.util.InputMismatchException Using Double使用 Double 的线程“主”java.util.InputMismatchException 中的异常
【发布时间】:2023-04-09 16:26:01
【问题描述】:
import java.util.Scanner;
import java.text.DecimalFormat;

public class palomba2 {

    public static void main(String args[]) {

        Scanner keyboard = new Scanner(System.in);
        //allows input to be read
        // VOLUME OF A PRISM

        int prLngth, prWdth, prHght, prVol;

        System.out.print("Enter the length of the prism in feet: ");
        prLngth = keyboard.nextInt();

        System.out.print("Enter the width of the prism in feet: ");
        prWdth = keyboard.nextInt();

        System.out.print("Enter the height of the prism in feet: ");
        prHght = keyboard.nextInt();

        prVol = prLngth * prWdth * prHght;

        System.out.print("The volume of the prism is " + prVol);
        System.out.print(" feet.");
        System.out.println("");
        System.out.println("");
        // AREA/CIRCUMFERENCE OF A CIRCLE

        DecimalFormat format = new DecimalFormat("0.##");

        double radius, circ, area;

        System.out.print("Enter the radius of a circle rounded to the nearest hundreth: ");
        radius = keyboard.nextInt();

        circ = 2 * radius * 3.14159265358979;
        area = radius * radius * 3.14159265358979;

        System.out.print("The circumference of the circle is " + format.format(circ));
        System.out.print("units.");
        System.out.print("The area of the circle is " + format.format(area));
        System.out.print("units.");
    }
}

在我输入圆的半径之前,我的代码中的所有内容都有效。输入半径后出现错误消息。

 Enter the radius of a circle rounded to the nearest hundreth: 2.12

 Exception in thread "main" java.util.InputMismatchException
     at java.base/java.util.Scanner.throwFor(Scanner.java:860)
     at java.base/java.util.Scanner.next(Scanner.java:1497)
     at java.base/java.util.Scanner.nextInt(Scanner.java:2161)
     at java.base/java.util.Scanner.nextInt(Scanner.java:2115)
     at palomba2.main(palomba2.java:52)

【问题讨论】:

  • 欢迎您。你知道java.util.InputMismatchException 是什么吗?如果不是,是时候阅读它了:-)

标签: java compiler-errors double runtime-error


【解决方案1】:

您键入了一个双变量 2.12 但试图将其读取为 int :输入不匹配 -> InputMismatchException


另外,变量radius 的类型是double,所以你不能给它一个int 值(你也需要:

  • radius = keyboard.nextDouble();

但最好使用radius = Double.parseDouble(keyboard.nextLine()) 来消耗返回字符(int 相同:int val = Integer.parseInt(keyboard.nextLine())

【讨论】:

    【解决方案2】:

    你只需要改变:

    radius = keyboard.nextInt();

    到:

    radius = keyboard.nextDouble();

    因为变量radius 被声明为double 它应该是这样的:

    double radius, circ, area;
    System.out.print("Enter the radius of a circle rounded to the nearest hundreth: ");
    radius = keyboard.nextDouble();
    

    【讨论】:

    • 我觉得自己很愚蠢。在阅读我的代码一百万次后,我注意到了这一点。傻我。这是我编码的第二天,所以我可能会很想念这些简单的事情。感谢您的帮助!
    • "因为变量 radius 被声明为 double" 不完全是,这是因为用户提供了double 但代码被设计为通过nextInt() 读取int 所以它无法处理.12 部分。
    猜你喜欢
    • 2015-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多