我实现了一个计数器方法,它始终返回一个递增的数字。但用户可以给出愿望format 、 2 位数字、 3 位数字或任何他想要的数字。
格式是字符串的标准 String.format() 类型,如 %02d
或 %5d
。当达到最大值时,计数器应重置为 0。
如何找出给定 format 可以表示的最大值?
int counter = 0;
private String getCounter(String format){
if(counter >= getMaximum(format)){
counter = 0;
}
else {
counter++;
}
return String.format(format, counter);
}
private int getMaximum(String format){
//TODO ???
//Format can be %02d => should return 100
//Format can be %05d => should return 100000
}
最佳答案
尚未验证此代码,但类似的内容应该适用于错误检查
String str = fullresultText.replace ("%", "").replace("d", "");
maxVal = Math.pow (10, Integer.parseInt (str));
关于java - String.format() 获取最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19110103/