由于某种原因,此字符串格式无效。它说frameCount应该是一个对象。我目前没有导入任何库。有谁知道它为什么会出错?
frameRate是一个String,frameCount是一个int
frameRate = String.format("FPS %s", frameCount);
答案 0 :(得分:1)
%s
是字符串使用%d
的占位符。
String.format()
也是构建字符串的最慢方法。使用Stringbuilder或仅使用+
运算符的简单情况。
frameRate = "FPS " + frameCount;
答案 1 :(得分:0)
也许尝试打印一个int? 而不是:
frameRate = String.format("FPS %s", frameCount);
将其更改为:
frameRate = String.format("FPS %d", frameCount);
答案 2 :(得分:0)
答案 3 :(得分:0)
使用适当的placehodler作为int
的{{1}}原语:
%d
或保留当前格式规则并将frameRate = String.format("FPS %d", frameCount);
int
转换为字符串以适合frameCount
占位符:
%s
答案 4 :(得分:0)
您很可能会使用旧版本的Java(1.5之前版本),其中参数不会自动自动装箱。更正格式说明符,您可以使用
frameRate = String.format("FPS %d", frameCount);
答案 5 :(得分:0)
如果您需要将frameCount
保留为整数(由于要求),您可以将其转换为字符串,但这会不必要地增加一步并降低代码性能。
frameRate = String.format("FPS %s", String.valueOf(frameCount));