小编典典

我究竟做错了什么?Java IllegalFormatConversionException

java

我有一些代码来计算圆的属性:

package circleinfo;

import java.util.Scanner;

public class Circleinfo {

    public static void main(String[] args) {

        Scanner input=new Scanner(System.in);
        int r;

        System.out.print("Enter the radius of the circle to find circumference, diameter, and area\n");

        r = input.nextInt();

        System.out.printf("The circumference is %f\n",(2*r*Math.PI));
        System.out.printf("The diameter is %f\n",(r*2));
        System.out.printf("The area is %f\n",(r*r*Math.PI));

    }
}

它计算周长,但不计算其余部分。

Enter the radius of the circle to find circumference, diameter, and area

10

The circumference is 62.831853

Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.Integer
    at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4045)
    at java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2761)
    at java.util.Formatter$FormatSpecifier.print(Formatter.java:2708)
    at java.util.Formatter.format(Formatter.java:2488)
    at java.io.PrintStream.format(PrintStream.java:970)
    at java.io.PrintStream.printf(PrintStream.java:871)
    at circleinfo.Circleinfo.main(Circleinfo.java:30)
The diameter is Java Result: 1

阅读 6

收藏
2020-11-19

共1个答案

小编典典

r是一个intr*2也一个int,这意味着不能在第二个打印语句%f中使用。试试%d那里。

回想一下,这%f是浮点数,而%d整数。在的文档中对此进行了概述Formatter(请参阅“
格式化字符串语法”)。

2020-11-19

回答这个问题

Nothing to preview