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

© 邵阳 中级黑马   /  2012-7-14 19:49  /  2301 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 邵阳 于 2012-7-15 08:33 编辑

写了个小程序,目的是得出在一个字符串中有另一个字符串的个数,但是却是无限循环为什么啊?
class Test
{
        public static void show()
        {
                String str="abcdabcdabcd";
                String s1="ab";
                int count=0;
                System.out.println("........."+str.indexOf(s1,1));
                int i=0;
                while(i<str.length())
                {
                        if (str.indexOf(s1,i)>=0)
                        {
                                int temp=str.indexOf(s1,i)+1;
                                i=temp;
                        }
                        count=count+1;
                        System.out.println(count);
                }
                System.out.println(count);
        }
        public static void main(String[]args)
        {
                show();
        }
}
结果:
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759

8 个回复

正序浏览
黄昆 中级黑马 2012-7-15 00:25:36
9#
while(i<str.length())
                 {
                         if (str.indexOf(s1,i)>=0)/*哥们,你这个计数器都搞错了吧?你这个计数器只要满足i<str.length();
                        计数器就会自增,而这个条件会一直满足,所以就是死循环喽。你把计数器放在if循环语句内,再加上一个break,就ok了。
                        */
                         {
                                 int temp=str.indexOf(s1,i)+1;
                                 i=temp;
                         }
                         count=count+1;
                         System.out.println(count);
                 }
我帮你改了一下,你看看行不?
public class Demo
{
        public static void show()
        {
                String str="abcdabcdabcd";
                String s1="ab";
                int count=0;
                System.out.println("........."+str.indexOf(s1,1));
                int i=0;
                while(i<str.length())
                {
                        if (str.indexOf(s1,i)>=0)
                        {
                                int temp=str.indexOf(s1,i)+1;
                                i=temp;
                                count=count+1;
                         }
                else
                     break;
                }
                System.out.println(count);
        }
        public static void main(String[]args)
        {
                show();
        }
}
回复 使用道具 举报
{:soso_e103:}为什么无限循环勒?
因为你没跳出循环
回复 使用道具 举报
帮你把代码改进为:
class Test2 {
public static void show() {
  String str = "abcdabcdabcd";
  String s1 = "ab";
  int count = 0;
  System.out.println("........." + str.indexOf(s1, 1));
  int i = 0;
  // while(i<str.length())
  // {
  while (str.indexOf(s1, i) >= 0) {                             //可以这样加判断条件while((i=str.index(s1,i)!=-1){
   int temp = str.indexOf(s1, i) + 1;                         //                                                 i+=s1.length();
   i = temp;                                                               //                                               count++;
   count = count + 1;
  }
  System.out.println(count);
}
public static void main(String[] args) {
  show();
}
}

回复 使用道具 举报
本帖最后由 庄星睿 于 2012-7-14 23:26 编辑

class Test
{
        public static void show()
        {
                String str="abcdabcdabcd";
                String s1="ab";
                int count=0;
                System.out.println("........."+str.indexOf(s1,1));
                int i=0;
                while(i<str.length()) // 这里有问题
                {
                        if (str.indexOf(s1,i)>=0)
                        {
                                int temp=str.indexOf(s1,i)+1;
                                i=temp;
                        }
                        count=count+1;
                        System.out.println(count);
                }
                System.out.println(count);
        }
        public static void main(String[]args)
        {
                show();
        }
}

while(i<str.length())i的值等于你索引字符串中的角标值,如果返回-1说明字符串不存在,但如果找到了,他返回的角标值最大值是str.length()-1,所以永远都小于str.length(),所以你写的循环相当于 while(true)无限循环了,还有你这么写有点麻烦,下面是我在你代码基础上改后的:
class Test
{
        public static void show(String s1,String s2)
        {  
              String str_max=s1.length()>s2.length()?s1:s2; //判断字符串的大小
              String str_min=str_max==s1?s2:s1;
   
              int i=0,count=0;
             while((i=str_max.indexOf(str_min,i))!=-1)
             {
                     count++;
                     i=i+str_min.length();
              }
   
             System.out.println(count);
   
}
      
     public static void main(String[]args)
     {
         String str="abcdcabdcabdab";
         String s1="ab";
          show(str,s1);
    }
}

回复 使用道具 举报
邵阳 发表于 2012-7-14 21:54
哥们找不到的话是-1啊。然后不满足条件,怎么还执行啊啊。不理解

兄弟运行过我改的程序吗?
如果找不到返回-1,则跳出循环,直接输出0。
运行一下就明白了!
回复 使用道具 举报
韦念欣 发表于 2012-7-14 20:56
class Test
{
        public static void show()


哥们找不到的话是-1啊。然后不满足条件,怎么还执行啊啊。不理解
回复 使用道具 举报
本帖最后由 韦念欣 于 2012-7-14 20:59 编辑

class Test
{
        public static void show()
        {
                String str="abcdabcdabcd";
                String s1="ab";
                int count=0;
                System.out.println("........."+str.indexOf(s1,1));
                int i=0;
                while(i<str.length())
                {
                        if (str.indexOf(s1,i)>=0)
                        {
                                int temp=str.indexOf(s1,i)+1;
                                i=temp;
                                count=count+1;
                                System.out.println(count);
                        }
                        else                        // 当查找到末尾后,找不到s1在str上的字串,必须要跳出循环体,否则会死循环
                                break;
                }
                System.out.println(count);
        }
        public static void main(String[]args)
        {
                show();
        }
}

回复 使用道具 举报
本帖最后由 丁二跃 于 2012-7-14 21:20 编辑

错了 删了 ……
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马