Hi All
请教一个问题,纠结半天了;
    现有一个需求,要生成4位的流水号,如“0001、0002、0003......”,一直到“9999”;
    如果超过“9999”,则首位变成字母,如“A000、A001、A002,”,即10000=A000,11000=B000....26000=Z000..27000=AZ00,以此类推,
    求各位大神,给个指引吧

解决方案 »

  1.   

    27000=AZ00,27000怎么就跳到了AZ00?这个规则看不懂
      

  2.   

    后面说的字母替换没看懂,前面的我简单的写了个代码,给你做个引子,你可以参考下,希望对你有用
    String[][][][] arr = new String[10][10][10][10];
    Set<String> set = new TreeSet<String>();// 存放流水号
    for (int i = 0; i < arr.length; i++)
    {
    for (int j = 0; j < arr[i].length; j++)
    {
    for (int k = 0; k < arr[i][j].length; k++)
    {
    for (int z = 0; z < arr[i][j][k].length; z++)
    {
    String tmp = String.valueOf(i) + String.valueOf(j)
    + String.valueOf(k) + String.valueOf(z);
    set.add(tmp);
    }
    }
    }
    }
    int i = 0;
    for (String num : set)
    {
    if (i % 10 == 0)
    System.out.println("");
    System.out.print(num + "\t");
    i++;
    }
    输出结果:
    0000 0001 0002 0003 0004 0005 0006 0007 0008 0009
    0010 0011 0012 0013 0014 0015 0016 0017 0018 0019
    0020 0021 0022 0023 0024 0025 0026 0027 0028 0029
    0030 0031 0032 0033 0034 0035 0036 0037 0038 0039
    0040 0041 0042 0043 0044 0045 0046 0047 0048 0049
    0050 0051 0052 0053 0054 0055 0056 0057 0058 0059
    0060 0061 0062 0063 0064 0065 0066 0067 0068 0069
    0070 0071 0072 0073 0074 0075 0076 0077 0078 0079
    ...
    9940 9941 9942 9943 9944 9945 9946 9947 9948 9949
    9950 9951 9952 9953 9954 9955 9956 9957 9958 9959
    9960 9961 9962 9963 9964 9965 9966 9967 9968 9969
    9970 9971 9972 9973 9974 9975 9976 9977 9978 9979
    9980 9981 9982 9983 9984 9985 9986 9987 9988 9989
    9990 9991 9992 9993 9994 9995 9996 9997 9998 9999
      

  3.   

    //简单写了一个工具类,希望楼主能看懂
    public class SerialNumber { /** 二十六进制 */
    public static final int RADIX = 26;
    /** 流水号的长度  */
    public static final int DIGIT = 4;
    /** 字母表 */
    public static char[] LETTERs = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    /** 数字表 */
    public static char[] NUMBERs = {'0','1','2','3','4','5','6','7','8','9'};

    /** 判断有几位数字和几位字母的整数值 */
    private static final int[] DIGIT_LEVEL = new int[DIGIT+2];
    static{
    for(int i=1;i<DIGIT_LEVEL.length;i++){
    int multi = 1;
    for(int j=DIGIT+1-i;j>0;j--){
    multi*=10;
    }
    for(int j=i-1;j>0;j--){
    multi*=26;
    }
    DIGIT_LEVEL[i] = DIGIT_LEVEL[i-1] + multi;
    }
    }
    /** 最小值 */
    public static int MIN_VALUE = 1;
    /** 最大值 */ //736336
    public static int MAX_VALUE = DIGIT_LEVEL[DIGIT+1];
    /** 序列号的数值  */
    private static int value = MIN_VALUE;

    /**
     * 获取序列号的方法
     */
    public static String getNext(){
    int val = 0;
    synchronized (SerialNumber.class) {
    //数值溢出时重置为最小值
    if(value>DIGIT_LEVEL[DIGIT+1]){
    value = MIN_VALUE;
    }
    val = value++;
    }
    return format(val);
    }
    /** 将数值格式化成要求的格式 */
    public static String format(int value){
    char[] sn = new char[DIGIT];
    for(int i= DIGIT;i>=0;i--){
    if(value>=DIGIT_LEVEL[i]){
    value -= DIGIT_LEVEL[i];
    for(int j=DIGIT-1;j>=i;j--){
    sn[j] = NUMBERs[(value % 10)];
    value /= 10;
    }
    for(int j=i-1;j>=0;j--){
    sn[j] = LETTERs[(value % RADIX)];
    value /= RADIX;
    }
    break;
    }
    }
    return new String(sn);
    }

    private SerialNumber(){}

    /**
     * 测试用例
     */
    public static void main(String[] args) {
    for(int i=0;i<36000;i++){
    SerialNumber.getNext();
    }
    for(int i=0;i<10;i++){
    System.out.println(SerialNumber.getNext());
    }
    }}