我实现了一个计数器方法,它总是返回一个递增的数字。但用户可以给出希望的格式、2 位、3 位或任何他想要的。格式是 String 的标准 String.format() 类型,例如%02d
or %5d
。当达到最大值时,计数器应重置为 0。
如何找出可以用给定格式表示的最大值?
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
}