ecelipse里format方法老是报错,代码如下:同学复制我的代码在他的eclipse上运行就没问题…没财富了…

package P154;
import java.util.InputMismatchException;
import java.util.Scanner;
import org.apache.log4j.Logger;

public class Test {
private static Logger logger = Logger.getLogger(Test.class.getName());
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入被除数:");
try {
int num1 = input.nextInt();
logger.debug("输入被除数:" + num1);
System.out.print("请输入除数:");
int num2 = input.nextInt();
logger.debug("输入除数:" + num2);
System.out.println(String.format("%d / %d=%d", num1, num2, num1/ num2));
logger.debug("输出运算结果:"+String.format("%d / %d=%d", num1, num2, num1/ num2));
} catch (InputMismatchException e) {
logger.error("被除数和除数必须是整数", e);
} catch (ArithmeticException e) {
logger.error(e.getMessage());
} catch (Exception e) {
logger.error(e.getMessage());
} finally {
System.out.println("欢迎使用本程序");
}
}
}
这是报错提示

错误提示你的format方法参数不对,没有参数是4个的这个方法,你同学的如果对了,那肯定是对这个方法重写过了吧,你点Ctrl再点他的那个format方法,去看一下追问

书上也是这么写的……我把同学的代码也复制过了,都是format那报错。

追答

你把数字都改成包装类Integer,试一下,我看了下API,有一个方法
public static String format(String format, Object... args)

后边的参数是个枚举的Object类型,因为int是基本数据类型,不匹配,
也不要迷信课本,课本也有可能是错的

追问

我们还没学Interger,麻烦能写出代码吗?就format后面的。不甚感激。

追答

logger.debug("输出运算结果:"+String.format("%d / %d=%d", new Integer(num1), new Integer(num2), new Integer(num1/ num2)));

温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网