gpt4 book ai didi

尝试使用 printf 打印数组时出现 java.util.IllegalFormatConversionException

转载 作者:行者123 更新时间:2023-12-02 02:31:54 28 4
gpt4 key购买 nike

我正在尝试打印字符数组中的每个元素。

为什么我会收到 IllegalFormatConversionException

public class printf_function_test {     
public static void main(String[] args) {
int any = 5;
String object = "car";
char[] ch = {'3','5','6','9'};
System.out.println(ch);
System.out.printf(
"%d anything can happen anytime ,bought a %s and write"
+ "this number now %c",
any,
object,
ch
);
}
}

最佳答案

您正在将 char[] 作为单个字符格式 %c 传递。

Java 中的数组是一个对象,应将其格式化为其 String 表示形式(因此 %s)。

在您的情况下,由于数组的默认 String 表示不太可能是您想要的,因此您可以使用 %s 的组合并将其传递 Arrays.toString(ch)String.valueOf(ch) (感谢 Axel )。

关于尝试使用 printf 打印数组时出现 java.util.IllegalFormatConversionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46955748/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com