我正在尝试以两种方式编写一个方法 - 如果符号 $ 后面的字符是整数,那么我想转到第二个 if 语句并执行它。如果符号 $ 后面的字符是数字(或字母),那么我想将该数字设置为 16,并且每次使用新数字时,我都想将 setNumber 增加 1。这是我尝试过的:
for (i=0; i<anyLines.length; i++) {
int setNumber = 16;
// 1st IF statement
if (Character.isDigit(anyLines[i].charAt(the character after $)))
anyLines[i] = anyLines[i].replace("$","");
anyLines[i] = anyLines[i].replace(charAt(after zero),setNumber);
// Increment set number if a new digit is detected
}
else {
continue;
}
// 2nd IF statement
if (anyLines[i].isInteger(anyLines[i].charAt(the character after $))) {
anyLines[i] = anyLines[i].replace("$","");
anyLines[i] = Integer.toBinaryString(131072
+(Integer.parseInt(anyLines[i]))).substring(1,17);
}
else {
continue;
}
我不知道该怎么做。
像这样:
public static void main(String[] args) {
String s = "af$Aklj$4r8$7jlkf$;a4$";
char[] cs = s.toCharArray();
for (int i = 0; i < cs.length; i++) {
if (cs[i] == '$') {
if (i + 1 < cs.length) {
i++;
if (Character.isDigit(cs[i])) {
System.out.print("digit after $: ");
} else if (Character.isLetter(cs[i])) {
System.out.print("letter after $: ");
} else {
System.out.print("unhandled character after $: ");
}
System.out.println(String.copyValueOf(cs, i-1, 2));
}
}
}
}