【问题标题】:Detecting characters after symbols检测符号后的字符
【发布时间】:2011-05-24 04:41:25
【问题描述】:

我正在尝试以两种方式编写一个方法 - 如果符号 $ 之后的字符是整数,那么我想转到第二个 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;
            }

我不知道该怎么做。

【问题讨论】:

  • 也许是一些示例输入/输出组合?你意识到一个数字一个整数,对吧?那么有什么区别——多于一位的整数?
  • 不,区别是一个是整数一个是字母 如果检测到$1(它是一个整数),那么转到第二个IF语句。如果检测到 $A(它是一个字母),则转到第一个 IF 语句。

标签: java symbols character integer


【解决方案1】:

类似这样的:

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));
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2017-12-23
    • 1970-01-01
    • 1970-01-01
    • 2011-01-01
    • 2012-07-28
    • 1970-01-01
    • 2016-05-11
    • 1970-01-01
    • 2020-10-10
    相关资源
    最近更新 更多