生成固定位数的字母流水号?

superlyq006 2007-08-29 10:35:48
生成固定位数的字母流水号?
...全文
396 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
superlyq006 2007-08-29
  • 打赏
  • 举报
回复
类似这样的
AAAA
AAAB
AAAC
....
superlyq006 2007-08-29
  • 打赏
  • 举报
回复
写一下我自己写的,对比一下上面的,自己的好像比较有局限性。
谢谢:bao110908(长牙了,好痛)

char changeChar ='A';
int charint = 0;
String nowFeeCode = "ZEYJDO";
char tempChar = '0';
charint = (int)changeChar;

for(int count =0;count < nowFeeCode.length(); count++){
changeChar = nowFeeCode.charAt(count);
charint = (int)changeChar;

if (charint<=89){
tempChar = (char)(charint+1);
nowFeeCode = nowFeeCode.substring(0,count) + tempChar + nowFeeCode.substring(count+1);
tempChar = '0';

}
else{
tempChar = 'A';
nowFeeCode = nowFeeCode.substring(0,count) + tempChar + nowFeeCode.substring(count+1);

}
if (tempChar == '0'){
break;
}

}
System.out.println("Loop nowFeeCode = " + nowFeeCode);
createthread 2007-08-29
  • 打赏
  • 举报
回复
帮顶,接分
  • 打赏
  • 举报
回复
public class Test {

  public static void main(String[] args) {
    IdentifierAlpha id = new IdentifierAlpha(4, 'A', 'Z');
    for (int i = 0; i < 17575; i++) {
      id.next();
    }
    // 应该输出 AZZZ
    System.out.println(id.next());
    // 应该输出 BAAA
    System.out.println(id.next());
  }
}

class IdentifierAlpha {

  private char[] sequence;
  private char start;
  private char end;

  private IdentifierAlpha() {
  }

  public IdentifierAlpha(int bit, char start, char end) {
    if(start > end) {
      char tmp = end;
      end = start;
      start = tmp;
    }
    this.start = start;
    this.end = end;
    sequence = new char[bit + 1];
    sequence[sequence.length - 1] = (char) (start - 1);
    for (int i = sequence.length - 2; i > 0; i--) {
      sequence[i] = start;
    }
    sequence[0] = '_';
  }

  /**
   * 每次增加一个字母,并且循环使用,如 ZZZZ,下一个则为 AAAA
   * @return
   */
  public String next() {
    if (sequence == null) {
      return null;
    }
    sequence[sequence.length - 1] += 1;
    for (int i = sequence.length - 1; i > 0; i--) {
      if (sequence[i] > end) {
        sequence[i] = start;
        sequence[i - 1] = (i != 1) ? (char)(sequence[i - 1] + 1) : '_';
      } else {
        break;
      }
    }
    return new String(sequence, 1, sequence.length - 1);
  }
}

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧