首页 > 解决方案 > 使用 equalsIgnoreCase 返回“char 不能被尊重”

问题描述

我正在计算 numTimesE 方法中字符串短语 e 或 E 出现的次数。我在运行下面的代码时遇到了错误。我通过使用 || 修复了下面 在我的 if 语句中。我想知道为什么会出现这个错误,以及是否有可能的方法仍然使用 String.equalsIgnoreCase(),或者比 || 更简单的方法 在 if 语句中。

import java.util.Scanner;
public class Testing
{
    //numTimesE method
    public static int numTimesE(String phrase)
    {
        int countE = 0;
        for (int i = 0; i < phrase.length(); i++)
        {
            if (phrase.charAt(i).equalsIgnoreCase('e'))
                countE++;
        }//end of for
        return countE;

    }// end of numTimesE

    //main method
    public static void main(String[] args)
    {
        int num = numTimesE("Earl why are you yelling?");
        System.out.println(num);

    }//end of main
}//end of Testing

标签: java

解决方案


推荐阅读