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

本帖最后由 淡淡柠檬茶 于 2014-6-4 18:21 编辑

//获取一个字符串在另一个字符串中出现的次数
class  Strings
{
        public static void main(String[] args)
        {
                String str = "absbbsbsbasd";
               
                //System.out.println(getSubCount(str,"b"));//方法1 成立

                 s = getSubCount(str,"b");
                System.out.println(s);                                          //方法2 不成立 说不兼容类型
        }
        public static int getSubCount(String str,String key)
        {
                int count = 0;
                int index = 0;
                while((index = str.indexOf(key))!=-1)
                {
                        str = str.substring(index+key.length());
                        System.out.println("str="+str);
                        count++;
                }
                return count;
        }
        
}
请指教方法1和2的区别,谢谢

3 个回复

倒序浏览
变量s没有定义  应该改为 int s = getSubCount(str,"b");
回复 使用道具 举报
int  s = getSubCount(str,"b")
回复 使用道具 举报 2 0
好山炮的问题。。。。感谢楼上,我眼神没跟上
回复 使用道具 举报 1 1
您需要登录后才可以回帖 登录 | 加入黑马