A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© hailong5214 中级黑马   /  2017-3-21 23:02  /  1072 人查看  /  1 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

/*3.取出一个字符串中字母出现的次数。如:字符串:"abcdekka27qoq" ,
* 输出格式为:a(2)b(1)k(2).*/

public class Test2 {
        public static void main(String[] args) {
                StringBuffer s = new StringBuffer("aaabbb");

                for (int i = 0; i < s.length(); i++) {
                        int count = 1;
                        for (int j = i + 1; j < s.length(); j++) {
                                if (s.charAt(i) == s.charAt(j)) {
                                        count++;
                                        s = s.deleteCharAt(j);
                                }
                        }
                        System.out.print(s.charAt(i) + "(" + (count ) + ")");
                }
        }
}
我的结果是a(2)a(1)b(2)b(1)
知道错了但是不知道怎么改啊,有大神么

1 个回复

正序浏览
把打印放在第二个for里试试
来自宇宙超级黑马专属安卓客户端来自宇宙超级黑马专属安卓客户端
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马