生成字母数字系列

时间:2015-02-25 14:28:59

标签: java for-loop logic

我遇到基于给定数字生成数字的问题。实际上,一旦DAA001,DAA002,DAA003......DAA999系列填充了DA,我必须得到类似DAA999的系列,它必须生成DAB001...DAB999并且系列DAB填充DAB999 1}}它必须生成DAC001DAC999,直到DAZ001 toDAZ999为止。

这是我的代码,请帮助我。

String start="DA";
String driv[]={"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"};
String PrNumber="";
int  count=0;
if(driverprId<=999){
    count=0;
    start=start+driv[count];

    if(driverprId>=1&&driverprId<10){
        PrNumber=start+"00"+driverprId;
    }

    if(driverprId>=10&&driverprId<100){
        PrNumber=start+"0"+driverprId;
    }

    if(driverprId>=100&&driverprId<=999){
        PrNumber=start+driverprId;
    }
}

if(driverprId>999){
}

return PrNumber;

2 个答案:

答案 0 :(得分:0)

尝试类似:

int counter = 0;
for (int i=0;i<driv.length;i++) {//for each alphabet
    for (int j=0;j<999;j++) { //iterate till 999
         start=start+driv[i];//keep using DA A if i is 0 for 999 times
         ...

        if (counter == number) {//number is like number of cells you need
            return;
        }
        counter++;
    }
}

答案 1 :(得分:0)

这样的事情应该有效:

String start="DA";
String driv[]={"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"};
for(int i = 0; i < driv.length; i++) {
    for(int j = 1; j <= 999; j++) { 
        String numbers;
        if (j / 10 < 1) {
            numbers = "00" + j;
        } else if (j / 10 < 10) {
             numbers = "0" + j;
        } else {
             numbers = "" + j;
        }
        PrNumber = start + driv[i] + numbers;
}

仅限条件:

if (driverprId / 10 < 1) {
        numbers = "00" + j;
    } else if (driverprId / 10 < 10) {
         numbers = "0" + j;
    } else {
         numbers = "" + j;
    }