81,114
社区成员
发帖
与我相关
我的任务
分享
public class StringBizzare {
//CODE for a
final private int BEGIN_CODE = 97;
final private int END_CODE = 122;
final private int LENGTH = 26;
final private String DEFAULT = "a00";
private int currantIndex;
private int currantNumber;
public StringBizzare(String bizzare) throws Exception {
if(bizzare == null || bizzare.isEmpty()) {
parse(DEFAULT);
} else {
parse(bizzare);
}
}
private void parse(String bizzare) throws Exception {
if(bizzare == null || bizzare.isEmpty()) {
throw new Exception("Empty string, WTF!");
}
char firstChar = bizzare.charAt(0);
if (firstChar < BEGIN_CODE || firstChar > END_CODE) {
throw new Exception("Invalid string, WTF!");
}
try {
currantNumber = Integer.parseInt(bizzare.substring(1));
}
catch(Exception ex) {
throw new Exception("Invalid string, WTF!");
}
currantIndex = firstChar - BEGIN_CODE;
}
private String compose() {
char c = (char) (currantIndex + BEGIN_CODE);
String n = String.valueOf(currantNumber);
return c + (n.length() == 1 ? "0" + n : n);
}
public String increment(int count) {
currantNumber += count % 100;
currantIndex += count / 100 % LENGTH;
return compose();
}
@Override
public String toString() {
return compose();
}
public static void main(String[] args) throws Exception {
System.out.println(new StringBizzare(null));
System.out.println(new StringBizzare("b05").increment(105));
System.out.println(new StringBizzare("b05").increment(-102));
}
}
package topics_390910834;
public class StringIncrement1 {
public static void main(String[] args) {
char[] exclusionList = new char['A' - '9' - 1];
for (char cc = '9' + 1; cc < 'A'; cc++) {
exclusionList[cc - '9' - 1] = cc;
}
// System.out.println(exclusionList);
for (String ss = "A01"; !(ss.equals("Z99")); ss = increment(ss, '0',
'Z', exclusionList)) {
System.out.println(ss);
}
}
/**
* 让一串字符串自增
*
* @param inputString
* 输入的字符串
* @param startChar
* 开始字符,比如字符0
* @param endChar
* 结束字符,比如字符Z
* @param exclusionList
* 除外列表,比如字符9和字符A之间的[:;<=>?]
* @return 输出的字符串
*/
public static String increment(String inputString, char startChar,
char endChar, char[] exclusionList) {
char[] inputCharArray = inputString.toCharArray();
boolean carryFlag = false;
for (int ii = inputCharArray.length - 1; ii > 0; ii--) {
if (inputCharArray[ii] == endChar) {
inputCharArray[ii] = startChar;
carryFlag = true;
} else {
if (ii == inputCharArray.length - 1 || carryFlag) {
do {
inputCharArray[ii]++;
} while (exclusionList[0] <= inputCharArray[ii]
&& inputCharArray[ii] <= exclusionList[exclusionList.length - 1]);
}
}
}
return String.valueOf(inputCharArray);
}
}
package topics_390910834;
public class StringIncrement2 {
public static void main(String[] args) {
for (char cc = 'A'; cc <= 'Z'; cc++) {
for (int ii = 1; ii <= 99; ii++) {
System.out.printf("%s%02d\n", cc, ii);
}
}
}
}
public class Run {
//甲、乙、丙、丁、戊、己、庚、辛、壬、癸
public static final int[] skyBranch = new int[] { 0, 1, 2, 3, 4, 5, 6, 7,
8, 9 };
//子、丑、寅、卯、辰、巳、午、未、申、酉、戌、亥
public static final char[] earthBranch = new char[] { 'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l' };
public static void main(String[] args) {
for (int i = 0; i < 2020; i++) {
try {
caculate(i);
if ((i % 20) == 0) {
System.out.println("");
} else {
System.out.print(",");
}
} catch (RuntimeException e) {
System.out.println("Year" + i + " met exception.");
}
}
}
private static void caculate(int i) {
if (i < 4) {
throw new IllegalArgumentException(
"The starting year must be greater than 4");
}
int realYear = i - 4;
System.out.print("year" + i + " =[" + skyBranch[realYear % 10] + "]["
+ earthBranch[realYear % 12] + "]");
}
}
类似这个干支纪年法,随便改一改就OK了
String ss="123adbc";
char[] ca = ss.toCharArray();
char[] cap = new char[ca.length];
for(int i=0 ;i <ca.length;i++){
cap[i] = (char)(ca[i]+1);
}