编写一个用string类中的的indexOf方法,确定一个字符串在另一个字符串中出现次数的应用程序。。
我是这样写的:
public class Test
{
    public static void main(String[] args)
    {
        String s1="i";
        String s2="i an a i an a i";
        int d=0; 
        int count=0;
        while(i=-1)
        {
        public int indexOf(String s1,int d)
        {
            count=count+1;
        }
        System.out.println(count);
        }
    }
}

解决方案 »

  1.   

    你这是要重写indexOf()方法,还是要直接用indexOf()方法写程序呀
    看你写的问题是Z多
      

  2.   

    呆呆的问一下,while 里的循环条件可以  让i=-1么??
    其他程序的错误还蛮多的
      

  3.   

    重新写一个吧,都是错误。
    class test
    {
        public static void main(String[] args)
        {
            String s1="i";
            String s2="i an a i an a i";
            int d=0;
    int pos=0;
            int count=0;
    for (int i=0;i<s2.length() ;i++ )
    {
    pos=s2.indexOf(s1,d);
    if (pos>=0)
    {
    d=pos+1;
    count=count+1;
    }
    } System.out.println("count:"+count);    }
    }