Java system.out.format 想要 Object[] 数组而不是简单的变量?

标签 java format printf

我只是想用Javas printf 方法测试一些东西。自从我上次使用它以来已经有一段时间了,所以也许这现在是正常行为。

此代码是取自 http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html 的示例和维基百科。

问题是,它不会运行。

import java.util.Calendar;

public class TestPrintf {

    public static void main(String[] args) {

            System.out.printf("%s, %s", "Hello", "World!");

           // Writes a formatted string to System.out.
           System.out.format("Local time: %tT", Calendar.getInstance());
           // -> "Local time: 13:34:18"


    }

}

导致

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
The method printf(Locale, String, Object[]) in the type PrintStream is not applicable for the arguments (String, String, String)
The method format(String, Object[]) in the type PrintStream is not applicable for the arguments (String, Calendar)

at TestPrintf.main(TestPrintf.java:7)

我在 Ubuntu 上使用 Eclipse 版本:Mars.2 Release (4.5.2),Java 版本为 1.8.0_74。

我知道它曾经是这样工作的,但现在我必须提供一个包含变量的数组?如果我想格式化字符串和整数的混合格式怎么办?现在真的不需要混合类型的 Object[] 吗?

如果能了解为什么需要/改变这一点,我们将不胜感激。

最佳答案

我建议您检查项目中的编译器合规性级别是否设置为 1.5 或更高。

参见this问题以获取有关如何执行此操作的解释。

关于Java system.out.format 想要 Object[] 数组而不是简单的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36014460/

相关文章:

c - 如何在C中将数字矩阵打印成字符串?

java - 在 Java 中,是否有一种内置的或常见的 Stream 允许输出,比如说,5 位数据?

r - ggplot2:格式化图例类别

c - 解释C程序的输出

R如何在sprintf中输出 "\"

format - Lisp 多次格式化一个字符

java - 在 JPA 中定义关系属性

java - LuaJ 中的多个返回值

java - 使用 Spring Security 在 @WebMvcTest 中测试 JwtDecoder

javascript - 在javascript中格式化小数