我必须使用 Java 编写一个类代码,其中计算并打印出字母 E 的出现次数(包括两种情况)。这就是我所拥有的。
String sVerse = "As we enter our centennial year we are still young ";
System.out.println(sVerse);
int len3 = sVerse.length();
int countE = 0;
for (int d = 0; d <= len3; d++){
char e = sVerse.charAt(d);
d = d + 1;
if (e == 'e' || e == 'E')
{
countE = countE + 1;
}
else
{
countE = countE;
}
}
System.out.println(countE);
代码运行,字符串打印,但字符串打印后我得到这个错误:
java.lang.StringIndexOutOfBoundsException: String index out of range: 1258
at java.lang.String.charAt(Unknown Source)
at Unit4plus.main(Unit4plus.java:125)