2015-10-17 39 views
0

我可以从第二个打印输出获得输出 - 17.83,但是当我尝试使用第一个系统输出打印句子时,我不断收到IllegalFormatConversionException。非法格式转换问题

import java.util.Formatter; 
public class Q1 
{ 
    public static void main (String []args) 
    { 
     double r = Double.parseDouble(args[0]); 
     double a = Double.parseDouble(args[1]); 

     double area = 0.5*(Math.pow(r, 2))*(((a*(22/7))/180)- Math.sin((a*(22/7))/180)); 
     System.out.format("Area is %.2f", area + " when radius is " + r + " and angle is " + a); 
     System.out.printf("%.2f", area); 
+1

呃,半径为“+ r +”且角度为“+ a”的字符串“area +”不是浮点数。所以你不能用%.2f格式化它。 –

+0

在询问错误时总是发布完整的错误消息。不要套用它,因为您可能会忽略该消息包含的重要信息。你的格式字符串应该包含所有具体的字符串元素,而你的不是。 –

+1

更改为'System.out.format(“当半径为%.2f且角度为%.2f时,”区域为%.2f“,area,r,a);'并阅读javadoc https://docs.oracle。 COM/JavaSE的/教程/ JAVA /数据/ numberformat.html – earcam

回答

-1

System.out.format("Area is %.2f", area + " when radius is " + r + " and angle is " + a); 

应该是(为了编译)

System.out.format("Area is %.2f when radius is " + r + " and angle is " + a, area); 

,为了看起来不错:

System.out.format("Area is %.2f when radius is %.2f and angle is %.2f", area, r, a); 

在你的版本,你是路过作为第一个证明人的"Area is %.2f"吨和类似"6.28244564556 when radius is 1.0 and angle is 6.28244564556"。 (数字会有所不同)。显然,第二个参数不能被解析为浮点数。因此错误。

2

你的格式字符串应该包含所有具体的字符串元素,而你的不是。所以不

System.out.format("Area is %.2f", area + " when radius is " + r + " and angle is " + a); 

而是

System.out.format("Area is %.2f when radius is %.2f and angle is %.2f", area, r, a); 

在未来,询问错误时,请务必发布完整的错误消息。不要套用它,因为您可能会忽略包含该消息的重要信息