JAVA中商品6位递增编号怎么实现

来源:百度知道 编辑:UC知道 时间:2024/05/14 02:07:17
例如:000001 000002 000003 。。。。。
000009 000010 000011

贾府焦大,要是7位的编号,你的代码又要加点东西了,8位呢?居然有如此代码!!

String getCode(int id,int len){

String t = String.valueOf(id);

while(t.length()<len)

t="0"+t;

return t;

}

简单实现代码如下:
public class Test{
public static void main(String[] args)
{
for (int i=1;i<999999 ;i++ ){ //for循环
String code=i+"";
int leng=(code.trim()).length(); //定义长度
if(leng==1){
code="00000"+i;
}else if(leng==2){
code="0000"+i;
}else if(leng==3){
code="000"+i;
}else if(leng==4){
code="00"+i;
}else if(leng==5){
code="0"+i;
}
System.out.println("code:"+code);
}
}
}

要入库么?要入库的话建议用自带的自增列
只在取出来的之后前面添0显示

public class Test{
public static void main(String[] args)
{
for (int i=1;i<999999 ;i++ ){
String code=i+"";
int leng