gpt4 book ai didi

java - 字符串格式不起作用

转载 作者:行者123 更新时间:2023-12-01 23:15:09 24 4
gpt4 key购买 nike

我正在尝试使用一些新的 Java 字符串格式化技巧,非常基本的东西。它编译时没有任何错误消息,但不允许我执行代码。有人可以向我解释一下为什么这是错误的吗?提前致谢。

我的代码:

class TestingStringFormat {
public static void main(String args[]) {
System.out.printf("Total cost %-10d ; quantity %d\n", "ten spaces",5, 120);

for(int i=0; i<20; i++){
System.out.printf("%-2d: ", i, "some text here/n", 1);
}
}
}

最佳答案

在您的两个 printf 中,格式说明符的数量与您传入的参数数量不匹配。

例如,第一个 printf 内容为:

System.out.printf("Total cost %-10d ; quantity %d\n", "ten spaces",5, 120);

它需要一个整数,后跟另一个整数。您正在传递一个 String、一个 int 和另一个 int

在第二个 printf 中:

System.out.printf("%-2d: ", i,  "some text here/n", 1);

它需要一个整数。您正在传递一个 int、一个 String 和另一个 int

关于java - 字符串格式不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21357853/

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