本文介绍了PrintStream 类型中的 printf(String, Object[]) 方法不适用于参数 (...)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我通过简单调用 printf 会得到以下编译错误?我的代码:

Why do I get the following compilation error with a simple call to printf? My code:

import java.util.Scanner;

public class TestCodeBankAccInputs
{
    public static void main(String[] args)
    {
        String displayName = "Bank of America Checking";
        int balance = 100;
        System.out.printf("%s has %7.2f", displayName, balance);
    }
}

编译时出现以下错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
  The method printf(String, Object[]) in the type PrintStream is not applicable for the
    arguments (String, String, double)
  at TestCodeBankAccInputs.main(TestCodeBankAccInputs.java:9)

这是什么原因造成的,我该如何解决?

What's causing this and how can I fix it?

Help->About in Eclipse 提供以下信息:

Help->About in Eclipse gives following information:

面向 Web 开发人员的 Eclipse Java EE IDE.

Eclipse Java EE IDE for Web Developers.

版本:Indigo 发布版本号:20110615-0604

Version: Indigo ReleaseBuild id: 20110615-0604

我安装的JDK是JDK1.6.0_27

The JDK I installed is JDK1.6.0_27

我已经看到这个关于 String.format 的类似问题.一些用户认为这可能是构建问题,但看起来我已经更新了版本.

I have seen this similar issue regarding String.format. Some users have suggested it might be a build issue, but looks like I have updated versions.

推荐答案

检查Compiler compliance level 是否为您的项目设置为至少 1.5:

Check that the Compiler compliance level is set to at least 1.5 for your project:

项目>属性>Java编译器

Project > Properties > Java Compiler

如果未设置 Enable project specific settings,请使用该页面上的 Configue Workspace Settings... 链接检查全局 编译器合规性级别.

if Enable project specific settings is not set, use the Configue Workspace Settings... link on that page to check the global Compiler compliance level.

这篇关于PrintStream 类型中的 printf(String, Object[]) 方法不适用于参数 (...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 09:44