关于java中的printf()

domimasi 2007-12-02 06:08:02
下面这段代码:
import java.util.*;
public class Retirement
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.println("Please insert a nuber");
double num=in.nextdouble();
System.out.printf("%8d",num);

}
}
这段代码,我怎么看也没有错,可是就是不能运行,出错提示是这么说的:
xception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Double
at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source)
at java.util.Formatter$FormatSpecifier.printInteger(Unknown Source)
at java.util.Formatter$FormatSpecifier.print(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at Retirement.main(Retirement.java:9)
有没有高手知道啊,帮我回答下啊
...全文
2599 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
DANJANE 2011-07-05
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 lykax 的回复:]

以上回答似乎一个都不对。
C: printf("%8.3f",num):-->
Java: System.out.ptintf("%1$f8.3 ", num);
%1$ 表示第一个参数(num)
f 表示浮点数。f和g的意思和C相同。其它:d-十进制整数,h-八进制整数,x-十六进制,t-时间/日期,s-字符串,b-逻辑值,h-哈希值
8.3对于浮点数就是宽度和精度,其它类型的……
[/Quote]
确实如此
ponlya 2008-12-06
  • 打赏
  • 举报
回复
呵呵!看样子……C的功底还要加强呀!
lykax 2008-08-31
  • 打赏
  • 举报
回复
以上回答似乎一个都不对。
C: printf("%8.3f",num):-->
Java: System.out.ptintf("%1$f8.3 ", num);
%1$ 表示第一个参数(num)
f 表示浮点数。f和g的意思和C相同。其它:d-十进制整数,h-八进制整数,x-十六进制,t-时间/日期,s-字符串,b-逻辑值,h-哈希值
8.3对于浮点数就是宽度和精度,其它类型的含义不同。
tm-时间的分钟数
tB-月份
tb-月份简称
...
空格-表示填充符

Java的printf比C的pringtf功能要多。
try__again 2008-04-06
  • 打赏
  • 举报
回复
难怪,1.4的api里找不到这个
wensheng_zh2007 2007-12-02
  • 打赏
  • 举报
回复

import java.util.*;
public class Retirement
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.println("Please insert a nuber");
double num=in.nextDouble();
System.out.print(num);

}
}

这样也可
W51075007 2007-12-02
  • 打赏
  • 举报
回复
import   java.util.*;   
import java.util.Scanner;
public class Retirement
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.println("Please insert a nuber");
double num=in.nextDouble(); //注意,你这里写成了nextdouble
System.out.printf("%8f",num);
}
}


JAVA的System.out.printf是用于格式化输出的,从C借鉴过来的!从JDK5.0版本开始借鉴过来的!
%8d 是用8位十进制数表示,%8f才是浮点数输出
W51075007 2007-12-02
  • 打赏
  • 举报
回复
JAVA的System.out.printf是用于格式化输出的,从C借鉴过来的!从JDK5.0版本开始借鉴过来的!
hu437 2007-12-02
  • 打赏
  • 举报
回复
晕,写完发现居然已经有人在我之前答出来了~~
zhangbaokun 2007-12-02
  • 打赏
  • 举报
回复
其实我也不懂。。哈哈
hu437 2007-12-02
  • 打赏
  • 举报
回复
一二楼的朋友估计没有看过J2SE 5

知识还停留在1.4的阶段

楼主的代码错误主要有两点
double num=in.nextdouble(); //这里面的nextdouble()就为nextDouble();
System.out.printf("%8d",num); //这个地方的是d出错了,num是Double,d代表的是int型,d改为f就对了

System.out.printf("%8f",num);
liuliu20036 2007-12-02
  • 打赏
  • 举报
回复
import java.util.*;

public class Retirement {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Please insert a nuber");
// int num = in.nextInt();
double num = in.nextDouble();
System.out.printf("%8f", num);

}
}
num为double 类型,所以要改为%8f的类型
java.util.IllegalFormatConversionException这个异常是你一开始num为double类型的,你的输出格式为int类型的,二者不相符而抛出的
iskyshop 2007-12-02
  • 打赏
  • 举报
回复
抱歉,楼上提醒的是,不过确实有不少地方有些用c习惯的人自定了这个函数!所以楼上也不要这么说别人哈!
zhangbaokun 2007-12-02
  • 打赏
  • 举报
回复
不懂就别说
printf
public PrintStream printf(String format,
Object... args)
A convenience method to write a formatted string to this output stream using the specified format string and arguments.
An invocation of this method of the form out.printf(format, args) behaves in exactly the same way as the invocation

out.format(format, args)

Parameters:
format - A format string as described in Format string syntax
args - Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by the Java Virtual Machine Specification. The behaviour on a null argument depends on the conversion.
Returns:
This output stream
Throws:
IllegalFormatException - If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the Details section of the formatter class specification.
NullPointerException - If the format is null
Since:
1.5

--------------------------------------------------------------------------------

printf
public PrintStream printf(Locale l,
String format,
Object... args)
A convenience method to write a formatted string to this output stream using the specified format string and arguments.
An invocation of this method of the form out.printf(l, format, args) behaves in exactly the same way as the invocation

out.format(l, format, args)

Parameters:
l - The locale to apply during formatting. If l is null then no localization is applied.
format - A format string as described in Format string syntax
args - Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by the Java Virtual Machine Specification. The behaviour on a null argument depends on the conversion.
Returns:
This output stream
Throws:
IllegalFormatException - If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the Details section of the formatter class specification.
NullPointerException - If the format is null
Since:
1.5
iskyshop 2007-12-02
  • 打赏
  • 举报
回复
printf这个不是Java的系统函数,很多时候是自己定义的,其实就是System.out.print
snake3201 2007-12-02
  • 打赏
  • 举报
回复
有些书很恶心在前面把System.out.print给定义成printf使用,其实还是System.out.print的功能,你现在的问题应该是看到这种很恶心的书被误导了

62,631

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧