当前位置: 代码迷 >> Java相关 >> 看看哪里错了
  详细解决方案

看看哪里错了

热度:113   发布时间:2009-10-27 16:04:26.0
看看哪里错了
输出单词里出现最频繁的字母, 为什么程序总是瘫痪
    public char mostFrequent()
    {
        int count=0;
        int maxCount=0;
        char frequent='a';
        char[] nameArray;
        char one, two;
        int l=0, p=1;
        String str1=line.toLowerCase();
        nameArray=str1.toCharArray();
        
        for(int i=0; i<line.length(); i++)
        {     
         for(int t=0; t<line.length(); t++)
            {
            one=nameArray[i];
            two=str1.substring(l, p).toLowerCase().charAt(0);
            if(one==two)
               count++;  
             l++;
                p++;

         }
         if(count>maxCount)
            {
            maxCount=count;
            frequent=nameArray[i];
         }
        }
        return frequent;
    }

----------------解决方案--------------------------------------------------------
你的问题出在count--->一直都在加
    for(int i=0; i<line.length(); i++)   
        {   
            int count=0;   //放这里。外循环第(2、3、4、5....)次的时候重新统计
----------------解决方案--------------------------------------------------------
这题用正则表达式做,不用这么麻烦
----------------解决方案--------------------------------------------------------
  相关解决方案