各位大神,题目是:统计指定字符串中字符 ‘a’ 出现的次数...为什么if ( s[i]=="a") 不可以,要改成if(s.charAt(i))=='a')????
public class HelloWorld {
public static void main(String[] args) {
// 定义一个字符串
String s = "aljlkdsflkjsadjfklhasdkjlflkajdflwoiudsafhaasdasd";
// 出现次数
int num = 0;
// 循环遍历每个字符,判断是否是字符 a ,如果是,累加次数
for ( int i=0;i<s.length();i++ )
{
// 获取每个字符,判断是否是字符a
if ( s[i]=="a") {
// 累加统计次数
num++;
}
}
System.out.println("字符a出现的次数:" + num);
}
}