Java中System.out.printf参数传递错误分析与修正

在Eclipse中使用System.out.printf方法进行格式化打印时,如:System.out.printf("%5d", i),

会提示错误描述:The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (String,int)

此处的int可以换成其他基本数据类型如double,long等等;

查看printf()方法定义的路径为:java.io.PrintStream.printf(Locale, String, Object...)和java.io.PrintStream.printf(String, Object...),也就是所在的包java.io,类为PrintStream.class。

错误和JDK的本版相关,具体的原因是Java Compiler中Compiler comlicance level的数值太低。将Compiler comlicance level设置为不小于1.5,重新建立工程即可。

方法:Project >> Properties >> Java Compiler >> Compiler comlicance level。

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

PS:初学Java,仅供参考。


public class Test3 { static class Dog{ private String name = null; int health = 60; private int breed = 0; public Dog() { this("默认名字",0, 0); } public Dog(String name,int health, int breed) { setName(name); this.health = 60; this.breed = 0; } public String getName(){return name;} public int getHealth() {return health;} public int getBreed(){return breed;} public void setName(String newName){ if(newName!=null){ this.name = newName; } } public int dogHealth(int health){ if(health >100 || health < 1){ System.out.println("健康值应该在0到100之间,默认值是60."); } return health; } public void dogBreed(int breed){ if(breed == 1){ System.out.println("我的名字叫" + name + ",健康值是" + health + ",我是一只聪明的二哈!"); }if(breed == 2){ System.out.println("我的名字叫" + name + ",健康值是" + health + ",我是一只酷酷的金毛!"); }else { System.out.println("输入错误!!!"); System.exit(0); } } } static class Penguin{} public static void main(String[] args){ //Test3 test3 = new Test3(); Scanner scanner = new Scanner(System.in); System.out.println("欢迎您来到宠物店!"); System.out.print("请输入要领养宠物的名字:"); String name = scanner.next(); System.out.print("请选择要领养的宠物类型(1.狗狗 2.企鹅 ):"); int Num1 = scanner.nextInt(); System.out.print("请选择狗狗的品种(1.我是一只聪明的二哈 2.我是一只酷酷的金毛):"); int breedNum2 = scanner.nextInt(); System.out.print("请输入狗狗的健康值(1~100之间):"); int healthNum3 = scanner.nextInt(); Dog dog = new Dog(name,healthNum3,breedNum2); dog.dogHealth(healthNum3); dog.dogBreed(breedNum2); } }无法正确的输出健康值
最新发布
03-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值