我可以从第二次打印输出 - 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);
答案 0 :(得分:2)
您的格式字符串应包含所有具体的String元素,而您的格式不包含。所以不是
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);
将来,在询问错误时,总是会发布完整的错误消息。不要解释它,因为您可能会遗漏消息中包含的重要信息
答案 1 :(得分:-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"
。 (数字会有所不同)。显然,第二个参数可以解析为浮点数。因此错误。