java - 使用 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
解决方案
推荐阅读
- signalr - SignalR SocketException 仅在 Azure VM 上运行时
- python - ImportError:无法导入名称
仅在从 atom 运行文件时 - javascript - JavaScript Promise 没有返回正确的值
- php - JsGrid 和 laravel
- javascript - 打印 N-Queen 问题中的所有解决方案
- android - 更改 Google 地点自动填充预测的语言
- c# - DocumentDb DocumentClient 获取前 25 个文档
- azure-cosmosdb - MongoDB 计数没有给出真实的结果
- c# - app.config 错误“损坏或包含无效的 XML”?
- c# - 每个基类设计的接口