【发布时间】: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