首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >非法格式转换问题

非法格式转换问题
EN

Stack Overflow用户
提问于 2015-10-17 22:00:35
回答 2查看 41关注 0票数 0

我可以从第二个print out得到输出- 17.83,但是当我尝试使用第一个system out print语句时,我一直得到IllegalFormatConversionException。

代码语言:javascript
复制
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);
EN

回答 2

Stack Overflow用户

发布于 2015-10-17 22:06:05

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

代码语言:javascript
复制
System.out.format("Area is %.2f", area + " when radius is " + r + " and angle is " + a);

但更确切地说

代码语言:javascript
复制
System.out.format("Area is %.2f when radius is %.2f and angle is %.2f", area, r, a);

以后,在询问错误时,一定要发布完整的错误消息。不要转述它,因为您可能会遗漏消息包含的重要信息

票数 2
EN

Stack Overflow用户

发布于 2015-10-17 22:05:31

代码语言:javascript
复制
System.out.format("Area is %.2f", area + " when radius is " + r + " and angle is " + a); 

应该是(为了编译)

代码语言:javascript
复制
System.out.format("Area is %.2f when radius is " + r + " and angle is " + a, area);

为了看起来更漂亮:

代码语言:javascript
复制
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"。(数字会有所不同)。第二个参数显然不能被解析为浮点数。因此出现了错误。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33187479

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档