我究竟做错了什么?(What am I doing wrong? Java IllegalFormatConversionException)

编程入门 行业动态 更新时间:2024-10-19 21:35:12
我究竟做错了什么?(What am I doing wrong? Java IllegalFormatConversionException)

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

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

I have some code for calculating properties of a circle:

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)); } }

It calculates circumference, but not the rest.

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

                

最满意答案

r是一个int ,所以r*2也是一个int ,这意味着在第二个打印语句中不能使用%f 。 改为尝试%d 。

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

r is an int, so r*2 is also an int, meaning that in your second print statement %f cannot be used. Try %d there instead.

Recall that %f is for floating point numbers while %d is for integers. This is outlined in the documentation of Formatter (see Format String Syntax).

更多推荐

本文发布于:2023-07-26 15:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1277470.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错了   wrong   IllegalFormatConversionException   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!