我需要通过递增生成一系列数字。对于Ex,如果从001开始直到999,则不应该再次A01,直到A99接下来应该是B01。任何人都可以帮我写一个逻辑。应该使用Java代码实现。
答案 0 :(得分:1)
一次分配一批100个数字并使用它们,并在现有批次耗尽时分配新批次。所以像String getBatchPrefix()生成0,“A”,“B”...然后创建一个对象
class BatchIncrementer {
private final String prefix;
private final AtomicInteger incrementer;
BatchIncrementer(String pPrefix) {
incrementer = new AtomicInteger();
prefix = pPrefix;
}
Integer getNext() {
return incementer.getAndIncrement();
}
Boolean hasNext() {
return incrementer.get() < 100;
}
}
答案 1 :(得分:1)
简单且未完成,
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class GenerateProgresId {
private static String[] aChar = {"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"};
private static List<String> listA;
public GenerateProgresId() {
List<Number> list3 = new ArrayList<Number>();
List<Integer> list4 = new ArrayList<Integer>();
List<? extends Number> list5 = new ArrayList<Integer>();
List<? extends Number> list6 = list4;
list3.add(Integer.valueOf(333));
list4.add(Integer.valueOf(444));
//list5.add(Integer.valueOf(555)); // compile error
//list6.add(Integer.valueOf(666)); // compile error
Number n3 = list3.get(0);
Integer i4 = list4.get(0);
Number n5 = !list5.isEmpty() ? list5.get(0) : null;
Number n6 = list6.get(0);
System.out.printf("%s, %s, %s, %s%n", n3, i4, n5, n6);
int pomocInt = 1;
String pomocStr = "";
if (pomocInt < 10) {
pomocStr = "0" + pomocInt;
} else if (pomocInt < 100) {
pomocStr = String.valueOf(pomocInt);
} else {
pomocStr = String.valueOf(pomocInt);
}
System.out.println(pomocStr);
pomocInt = 12;
pomocStr = "";
if (pomocInt < 10) {
pomocStr = "0" + pomocInt;
} else if (pomocInt < 100) {
pomocStr = String.valueOf(pomocInt);
} else {
pomocStr = String.valueOf(pomocInt);
}
System.out.println(pomocStr);
pomocInt = 157;
pomocStr = "";
if (pomocInt < 10) {
pomocStr = "0" + pomocInt;
} else if (pomocInt < 100) {
pomocStr = String.valueOf(pomocInt);
} else if (pomocInt > 99) {
pomocStr = String.valueOf(pomocInt);
pomocStr = pomocStr.substring(pomocStr.length() - 2, pomocStr.length());
}
System.out.println(pomocStr);
listA = new ArrayList<String>();
listA.addAll(Arrays.asList(aChar));
System.out.println("From List");
System.out.println(GenerateProgresIdList(null));
System.out.println(GenerateProgresIdList(""));
System.out.println(GenerateProgresIdList("B"));
System.out.println(GenerateProgresIdList("AA"));
System.out.println(GenerateProgresIdList("AZ"));
System.out.println(GenerateProgresIdList("ZY"));
System.out.println(GenerateProgresIdList("ZZ"));
System.out.println("From String[]");
System.out.println(GenerateProgresIdString(null));
System.out.println(GenerateProgresIdString(""));
System.out.println(GenerateProgresIdString("B"));
System.out.println(GenerateProgresIdString("AA"));
System.out.println(GenerateProgresIdString("AZ"));
System.out.println(GenerateProgresIdString("ZY"));
System.out.println(GenerateProgresIdString("ZZ"));
}
public static String GenerateProgresIdList(String str) {
int lastChar = aChar.length - 1;
String retStr = "AA";
if (str != null) {
if (str.length() > 0) {
if (str.length() == 1) {
retStr = str + aChar[0];
} else if (str.length() == 2) {
int stChar = listA.indexOf(str.substring(0, 1));
int ndChar = listA.indexOf(str.substring(1, str.length()));
if ((stChar != lastChar) || (ndChar != lastChar)) {
if (ndChar == lastChar) {
retStr = listA.get(stChar + 1) + listA.get(0);
} else {
retStr = listA.get(stChar) + listA.get(ndChar + 1);
}
}
}
}
}
return retStr;
}
public static String GenerateProgresIdString(String str) {
String lastChar = aChar[aChar.length - 1];
String retStr = "AA";
if (str != null) {
if (str.length() > 0) {
if (str.length() == 1) {
retStr = str + aChar[0];
} else if (str.length() == 2) {
if ((!str.substring(0, 1).equals(lastChar)) || (!str.substring(1, str.length()).equals(lastChar))) {
String pos1 = str.substring(0, 1);
String pos2 = str.substring(1, str.length());
if ((pos2).equals(lastChar)) {
int heplInt = 0;
for (int i = 0; i < aChar.length; i++) {
if (aChar[i].equals(pos1)) {
heplInt = i + 1;
break;
}
}
retStr = aChar[heplInt] + aChar[0];
} else {
int heplInt = 0;
for (int i = 0; i < aChar.length; i++) {
if (aChar[i].equals(pos2)) {
heplInt = i + 1;
break;
}
}
retStr = pos1 + aChar[heplInt];
}
}
}
}
}
return retStr;
}
public static void main(String[] args) {
GenerateProgresId gpi = new GenerateProgresId();
}
}
答案 2 :(得分:1)
试试这个,
class Foo
{
private static int incr=0;
public static String getNo()
{
incr++;
if(incr%100==0)
incr++;
String no=String.valueOf(incr%100);
String str= String.valueOf((char)('A'+incr/100)) + new String(new byte[]{'0','0'},0,2-no.length()) +
String.valueOf(incr%100);
return str;
}
}
public class Test
{
public static void main(String args[]){
for(int i=1;i<=310;i++){
System.out.println(Foo.getNo());
}
}
}
答案 3 :(得分:1)
执行此操作的一种方法是分别制作第一个字符和第二个字符。
public SequenceGenerator {
private int lowerTwoDigits;
private char thirdDigit;
public SequenceGenerator () {
lowerTwoDigits = 0;
thirdDigit = '0';
}
public String nextElement() {
lowerTwoDigits++;
if (lowerTwoDigits == 100) {
lowerTwoDigits = 1;
thirdDigit++;
if (thirdDigit == '9' + 1)
thirdDigit = 'A';
}
String result = thirdDigit +
('0' + lowerTwoDigits / 10 ) +
('0' + lowerTwoDigits % 10 );
return result;
}
}
public class Main {
public static void main(String[] args) {
SequenceGenerator s = new SequenceGenerator();
int MAX = 99 * 36;
while (MAX >= 0) {
System.out.println(s.nextElement());
MAX--;
}
}
}
这应该是您要求的,我认为您可以理解nextElement方法中发生了什么。
答案 4 :(得分:1)
这是我的(笨拙但希望是正确的)尝试:
class Program {
public static void main(String[] args) {
Incrementer inc = new Incrementer();
while (inc.hasNext()) {
System.out.println(inc.getNext());
}
}
}
class Incrementer {
private final static char[] prefixes = { '0', '1', '2', '3', '4', '5', '6',
'7', '8', '9', 'A', 'B', 'C' };
//all the way to 'Z' if you want, but I'm lazy
private int currentPrefix = 0;
private int currentNumber = 0;
public boolean hasNext() {
if ((currentPrefix == (prefixes.length - 1)) && currentNumber > 99) {
return false;
}
return true;
}
// this may throw ArrayIndexOutOfBoundsException, check hasNext() first
public String getNext() {
String result;
if (currentNumber > 99) {
currentNumber = 1;
currentPrefix++;
}
result = "" + prefixes[currentPrefix];
if (currentNumber <= 9) {
result += "0";
}
result += currentNumber;
currentNumber++;
return result;
}
}
答案 5 :(得分:0)
你在找这样的东西..(未经测试)
char ch='a';
int iterations=999;
int count=0;
for(int i=0;i<iterations;i++){
if(i%100==0){
ch++;
count=0;
}
System.out.println(ch+""+count);
count++;
}
答案 6 :(得分:0)
这是一个简单的字母增量器,我相信你可以弄清楚其余的: - )
public static String incrementAlphabetic(String value, int position) {
value = value.toUpperCase();
char c = value.charAt(position);
c++;
boolean next = false;
if (c > 'Z') {
next = true;
c = 'A';
}
String n = value.substring(0, position)
+ String.valueOf(c)
+ value.substring(position + 1, value.length());
return (next && position > 0) ? incrementAlphabetic(n, position - 1) : n;
}
public static void main(String[] args) {
String start = "A";
for (int i = 0; i < 1000; i++) {
System.out.println(start = incrementAlphabetic(start, 0));
}
}