求一个判断字符串内容的逻辑

柯南8023 2017-02-10 10:15:13
计算出一个字符串中连续顺序的字母和数字的个数,比如456adefg中的456和efg为连续字符,还有就是挨在一起的相同字符最后也能统计一下,拜谢!
...全文
145 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ryuugu_rena 2017-02-10
  • 打赏
  • 举报
回复
简单点做就是做个判断,差不多够用了
SmarkMeng 2017-02-10
  • 打赏
  • 举报
回复
取Ascii 码做差,差为0或1则为相同或连续
eagle*man 2017-02-10
  • 打赏
  • 举报
回复
我来学习下,看看
ASCII_marvel 2017-02-10
  • 打赏
  • 举报
回复
不知能否用正则表达式解决
Leonis丶L 2017-02-10
  • 打赏
  • 举报
回复
引用 3 楼 lmx125254 的回复:
package com.ceshi;


import java.util.*;

/**
 * Created by Administrator on 2017/2/10.
 */
public class demo {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("请输入字符串:");
        String str = s.next();
        List list = new ArrayList();
        char[] zfc = str.toCharArray();
        int count = 0;
        int cishu = 0;
        Set<String> numStr = new HashSet<String>();
        Set<String> charStr = new HashSet<String>();
        for(int i=0;i<=zfc.length-1;i++){
            char  a = zfc[i];
            if (i != zfc.length - 1) {
                char b = zfc[i + 1];
                if (Character.isDigit(a) == Character.isDigit(b)) {
                    if (Character.isDigit(a)) {
                        if (b - a == 1) {
                            numStr.add(a+"");
                            numStr.add(b+"");
                        }
                    }else{
                        int x = Character.getNumericValue(a);
                        int y = Character.getNumericValue(b);
                        if(x +1 ==y){
                            charStr.add(x+"");
                            charStr.add(y+"");
                        }
                    }
                }else {
                    count += numStr.size();
                    cishu += charStr.size();
                    numStr.clear();
                    charStr.clear();
                }
            }else{
                count += numStr.size();
                cishu += charStr.size();
                numStr.clear();
                charStr.clear();
            }
        }
        System.out.println(count);
        System.out.println(cishu);
    }
}
挨在一起相同的没有判断
Leonis丶L 2017-02-10
  • 打赏
  • 举报
回复
package com.ceshi;


import java.util.*;

/**
 * Created by Administrator on 2017/2/10.
 */
public class demo {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("请输入字符串:");
        String str = s.next();
        List list = new ArrayList();
        char[] zfc = str.toCharArray();
        int count = 0;
        int cishu = 0;
        Set<String> numStr = new HashSet<String>();
        Set<String> charStr = new HashSet<String>();
        for(int i=0;i<=zfc.length-1;i++){
            char  a = zfc[i];
            if (i != zfc.length - 1) {
                char b = zfc[i + 1];
                if (Character.isDigit(a) == Character.isDigit(b)) {
                    if (Character.isDigit(a)) {
                        if (b - a == 1) {
                            numStr.add(a+"");
                            numStr.add(b+"");
                        }
                    }else{
                        int x = Character.getNumericValue(a);
                        int y = Character.getNumericValue(b);
                        if(x +1 ==y){
                            charStr.add(x+"");
                            charStr.add(y+"");
                        }
                    }
                }else {
                    count += numStr.size();
                    cishu += charStr.size();
                    numStr.clear();
                    charStr.clear();
                }
            }else{
                count += numStr.size();
                cishu += charStr.size();
                numStr.clear();
                charStr.clear();
            }
        }
        System.out.println(count);
        System.out.println(cishu);
    }
}

81,094

社区成员

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

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