我可以从第二个print out得到输出- 17.83,但是当我尝试使用第一个system out print语句时,我一直得到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);发布于 2015-10-17 22:06:05
你的格式化字符串应该包含所有的具体字符串元素,而你的没有。
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);以后,在询问错误时,一定要发布完整的错误消息。不要转述它,因为您可能会遗漏消息包含的重要信息
发布于 2015-10-17 22:05:31
这
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"。(数字会有所不同)。第二个参数显然不能被解析为浮点数。因此出现了错误。
https://stackoverflow.com/questions/33187479
复制相似问题